Default Values in Structure Definition

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
RobertRioja
User
User
Posts: 71
Joined: Thu May 02, 2019 3:57 am
Location: USA
Contact:

Re: Default Values in Structure Definition

Post by RobertRioja »

I understand that implementation might be difficult, but this would be a useful feature. For example, if you have a database of people, most of whom are from the US:

Code: Select all

Structure Person
  Name.s
  Age.l
  Country.s = "US"
EndStructure

Global People.Person
Now you can leave People.Country alone, or you can set it to another country as needed.
User avatar
Demivec
Addict
Addict
Posts: 4090
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Default Values in Structure Definition

Post by Demivec »

@RobertRioja: How about this?

Code: Select all

Structure Person
  Name.s
  Age.l
  Country.s
EndStructure

Global DefaultPersonValue.Person: DefaultPersonValue\Country = "US"  ;Define default for Person

Global People.Person = DefaultPersonValue ;Use default
Global OtherPeople.Person                         ;No default
This currently works in PureBasic just the way it is.
RobertRioja
User
User
Posts: 71
Joined: Thu May 02, 2019 3:57 am
Location: USA
Contact:

Re: Default Values in Structure Definition

Post by RobertRioja »

Yes, that would work. But it does not really address the original post, about having defaults in a structure. However, I do like your solution.
Thanks,
Robert
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Re: Default Values in Structure Definition

Post by kenmo »

As the author of this request, I don't really have a strong opinion about it 10 years later :lol:

Can I show a case where it's *needed*? No, of course not, it's not needed, you can always initialize your values in-line or by a constructor procedure. (But, we also had to *copy* structure values manually, until the team gave us CopyStructure()...)

In retrospect and knowing more about PureBasic, this feature might work nicely if implemented as part of InitializeStructure() -- whether called by the programmer or internally called by PB libraries.

Here's an example of how I imagine it working:

Code: Select all

Structure MySquare
  x.d       ; default to 0.0 as usual
  y.d       ; default to 0.0 as usual
  Width.d   = 5.0
  Height.d  = 5.0
  Scale.d   = 1.0
EndStructure


A.MySquare
Debug A\Scale ; would be 1.0

*B.MySquare = AllocateStructure(MySquare)
Debug *B\Scale ; would be 1.0

*C.MySquare = AllocateMemory(SizeOf(MySquare))
Debug *C\Scale ; would be 0.0 - not yet initialized!
;
InitializeStructure(*C, MySquare)
Debug *C\Scale ; would be 1.0
;
ClearStructure(*C, MySquare)
Debug *C\Scale ; would be cleared to 0.0
User avatar
diceman
User
User
Posts: 34
Joined: Tue Apr 10, 2018 9:42 pm

Re: Default Values in Structure Definition

Post by diceman »

That would be a nifty thing, indeed! 8)
Now these points of data make a beautiful line,
And we're out of Beta, we're releasing on time.
Post Reply