Page 1 of 2
Default Values in Structure Definition
Posted: Fri Feb 20, 2009 9:39 pm
by kenmo
Inspired by (
this more complicated post), wouldn't it be nifty if you could set initial structure values (of basic types) to chosen defaults in the definition?
Code: Select all
Structure TESTSTRUCT
This.l = 5
That.f = 10.0
Code.c = 'A'
MyText.s = "Yeah, defining strings in here would be trickier..."
EndStructure
May or may not be possible depending on how the compiler currently allocates structures, and it's not an urgent request at all.
But it'd be nice.
Posted: Sat Feb 21, 2009 10:11 am
by Seymour Clufley
I would prefer this:
Code: Select all
Structure Demo
timecast.i
state.s
things.i
thing.s[10]
EndStructure
SetStructureDefiner(Demo,@MyDefiningProcedure())
Procedure MyDefiningProcedure(*instance.Demo)
*instance\timecast = ElapsedMilliseconds()
*instance\state = "generic"
*instance\things = 5
For a = 1 to 5
*instance\thing[a] = "Untitled #"+Str(a)
Next
EndProcedure
Procedure AnalyseStructure(*dm.Demo)
If *dm\state = "generic"
Debug "Structure's starting values have been defined."
EndIf
EndProcedure
AnalyseStructure(@justmade.Demo)
Posted: Sat Feb 21, 2009 10:14 am
by Mistrel
I think structure defaults are a bad idea. What do you do if it's a public structure? With default offsets things could get unnecessarily complicated very quickly.
Just write an initialization function somewhere in your library and set the value yourself.
Posted: Sat Feb 21, 2009 11:34 am
by Seymour Clufley
Mistrel wrote:I think structure defaults are a bad idea. What do you do if it's a public structure? With default offsets things could get unnecessarily complicated very quickly.
Well, then you wouldn't set defaults.
Just write an initialization function somewhere in your library and set the value yourself.
The point of this feature request is that it would do something PB users can't really do with current code: automatically set values for a structure when it is first created. You may create a structure from any number of different procedures or on any number of different codelines. If you want the structure to have default or automatic values, you would have to call a "defining procedure" manually from every place that the structure was created, and sometimes that just isn't practical (see my example above).
This feature request, if implemented, would get around that with a single line of code.
Posted: Sat Feb 21, 2009 11:55 am
by Kaeru Gaman
not a
Structure hold values, but a
Structured Element.
a Structure is just a descriptive offset, it's nothing to assign a value to.
thus, defining a default value when defining a structure is completely off meaning.
it would be like defining every LONGs should be 5, but every WORDs should be 23.
when you assign a starting value to a Variable, you assign it upon
creation.
a more conveniant assigning method for Structured Elements upon creation would be nice.
but assigning values to a Structure itself is just physically incorrect.
Posted: Sat Feb 21, 2009 12:09 pm
by Mistrel
Kaeru Gaman, my point exactly.
This is a nonsensical shortcut that may feel like a luxury but will only make code more difficult to read and less maintainable.
kenmo, can you provide a real-world example where this would be the ideal solution for a problem? Perhaps some pseudo-code to reinforce your request?
Posted: Sat Feb 21, 2009 12:24 pm
by Kaeru Gaman
it wasn't Seymour's request. he postet a constructor that works upon creation.
Posted: Sat Feb 21, 2009 1:02 pm
by Mistrel
I corrected it to kenmo but my impression from Seymour's second post was that he is in favor of the change, correct me if I'm wrong:
Seymour Clufley wrote:This feature request, if implemented, would get around that with a single line of code.
Seymour, are you arguing for or against?

Posted: Sat Feb 21, 2009 5:50 pm
by Seymour Clufley
I'm arguing for it. I think it would be very useful in some circumstances - as long as it was optional.
I'm not advocating it for general use. Of course not! But as I said, there are some circumstances where it just isn't practical to manually set the values for a structure's fields, and this feature would deal with that.
Posted: Sat Feb 21, 2009 11:03 pm
by Mistrel
Seymour Clufley wrote:But as I said, there are some circumstances where it just isn't practical to manually set the values for a structure's fields, and this feature would deal with that.
Can you provide a real-world example where this would be the ideal solution for a problem? Perhaps some pseudo-code to reinforce the request?

Re: Default Values in Structure Definition
Posted: Mon Aug 12, 2013 10:53 am
by c4s
Sorry for digging up this old thread, but
+1 for the initial request.
Kaeru Gaman wrote:defining a default value when defining a structure is completely off meaning.
it would be like defining every LONGs should be 5, but every WORDs should be 23.
No, your comparison is wrong. All predefined data types (longs, words etc.) already have a default value, it's "0".
In a broader sense structures allow us to define our own data types. So it would really make sense to be able to define a default
behavior as well.
Re: Default Values in Structure Definition
Posted: Wed Aug 21, 2013 5:23 pm
by Danilo
c4s wrote:Sorry for digging up this old thread, but
+1 for the initial request.
Kaeru Gaman wrote:defining a default value when defining a structure is completely off meaning.
it would be like defining every LONGs should be 5, but every WORDs should be 23.
No, your comparison is wrong. All predefined data types (longs, words etc.) already have a default value, it's "0".
In a broader sense structures allow us to define our own data types. So it would really make sense to be able to define a default
behavior as well.
c4s, structures have also default values, as everything is set to "0". It is more complex with structured pointers, as you are able to
point to uninitialized or already initialized memory.
Code: Select all
Structure myStruct
i.i
l.l
b.b
s.s
EndStructure
struct1.myStruct
Debug struct1\i
Debug struct1\l
Debug struct1\b
Debug struct1\s
Debug "-----"
*struct2.myStruct = AllocateMemory( SizeOf(myStruct) )
If *struct2
Debug *struct2\i
Debug *struct2\l
Debug *struct2\b
Debug *struct2\s
EndIf
Debug "-----"
Writing your own init function is the way to go, so it is also clear in the code the structure gets initialized with values other than 0.
Re: Default Values in Structure Definition
Posted: Thu Aug 22, 2013 7:38 am
by Didelphodon
kenmo wrote:Inspired by (
this more complicated post), wouldn't it be nifty if you could set initial structure values (of basic types) to chosen defaults in the definition?
Code: Select all
Structure TESTSTRUCT
This.l = 5
That.f = 10.0
Code.c = 'A'
MyText.s = "Yeah, defining strings in here would be trickier..."
EndStructure
May or may not be possible depending on how the compiler currently allocates structures, and it's not an urgent request at all.
But it'd be nice.
+1
Re: Default Values in Structure Definition
Posted: Wed Sep 26, 2018 7:14 am
by Taz
kenmo wrote:Inspired by (
this more complicated post), wouldn't it be nifty if you could set initial structure values (of basic types) to chosen defaults in the definition?
Code: Select all
Structure TESTSTRUCT
This.l = 5
That.f = 10.0
Code.c = 'A'
MyText.s = "Yeah, defining strings in here would be trickier..."
EndStructure
May or may not be possible depending on how the compiler currently allocates structures, and it's not an urgent request at all.
But it'd be nice.
+1
Re: Default Values in Structure Definition
Posted: Sat Jan 19, 2019 6:58 pm
by SeregaZ
me too +1
