Default Values in Structure Definition

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Default Values in Structure Definition

Post 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.
Seymour Clufley
Addict
Addict
Posts: 1233
Joined: Wed Feb 28, 2007 9:13 am
Location: London

Post 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)
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post 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.
Seymour Clufley
Addict
Addict
Posts: 1233
Joined: Wed Feb 28, 2007 9:13 am
Location: London

Post 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.
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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.

Code: Select all

Define Nosebleed.l = 4711
a more conveniant assigning method for Structured Elements upon creation would be nice.
but assigning values to a Structure itself is just physically incorrect.
oh... and have a nice day.
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post 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?
Last edited by Mistrel on Sat Feb 21, 2009 12:59 pm, edited 1 time in total.
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

it wasn't Seymour's request. he postet a constructor that works upon creation.
oh... and have a nice day.
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post 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? :?
Seymour Clufley
Addict
Addict
Posts: 1233
Joined: Wed Feb 28, 2007 9:13 am
Location: London

Post 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.
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post 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?

:)
c4s
Addict
Addict
Posts: 1981
Joined: Thu Nov 01, 2007 5:37 pm
Location: Germany

Re: Default Values in Structure Definition

Post by c4s »

Sorry for digging up this old thread, but +1 for the initial request. :wink:
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.
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
User avatar
Danilo
Addict
Addict
Posts: 3037
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Default Values in Structure Definition

Post by Danilo »

c4s wrote:Sorry for digging up this old thread, but +1 for the initial request. :wink:
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.
User avatar
Didelphodon
PureBasic Expert
PureBasic Expert
Posts: 448
Joined: Sat Dec 18, 2004 11:56 am
Location: Vienna - Austria
Contact:

Re: Default Values in Structure Definition

Post 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
Go, tell it on the mountains.
Taz
User
User
Posts: 52
Joined: Sat Jan 20, 2018 5:28 pm
Location: Germany

Re: Default Values in Structure Definition

Post 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
SeregaZ
Enthusiast
Enthusiast
Posts: 619
Joined: Fri Feb 20, 2009 9:24 am
Location: Almaty (Kazakhstan. not Borat, but Triple G)
Contact:

Re: Default Values in Structure Definition

Post by SeregaZ »

me too +1 :)
Post Reply