Default Values in Structure Definition

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
RobertRioja
User
User
Posts: 80
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: 4260
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: 80
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: 2033
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: 66
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.
Rinzwind
Enthusiast
Enthusiast
Posts: 679
Joined: Wed Mar 11, 2009 4:06 pm
Location: NL

Re: Default Values in Structure Definition

Post by Rinzwind »

Yes please, low hanging fruit.. which already fell down and rotten away.
ps it would be nice if Fred would just mark stuff as DISAGREE or AGREE and add to public flexible roadmap.
Quin
Addict
Addict
Posts: 1131
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: Default Values in Structure Definition

Post by Quin »

Rinzwind wrote: Sat Jun 07, 2025 12:47 pm ps it would be nice if Fred would just mark stuff as DISAGREE or AGREE and add to public flexible roadmap.
+10000, this way we don't keep bumping requests for things that'll never happen.
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Default Values in Structure Definition

Post by Fred »

I don't know what the future hold, so it doesn't make sense to agree or not agree. Syntaxic sugar like this request have lower priority than things which can't be done otherwise.
Quin
Addict
Addict
Posts: 1131
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: Default Values in Structure Definition

Post by Quin »

Fair enough. Personally though, I would like to see a roadmap for features we know exist, like numeric maps, but that have yet to be added as syntax sugar.
User avatar
mk-soft
Always Here
Always Here
Posts: 6209
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Default Values in Structure Definition

Post by mk-soft »

NumericMap would be fine. Since there is already internal at PB.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
kenmo
Addict
Addict
Posts: 2033
Joined: Tue Dec 23, 2003 3:54 am

Re: Default Values in Structure Definition

Post by kenmo »

Fred wrote: Sat Jun 07, 2025 4:06 pmSyntaxic sugar like this request have lower priority than things which can't be done otherwise.
I agree with that, Fred (and I'm the one who started this thread, now 16 years ago, wow! :shock: )
SMaag
Enthusiast
Enthusiast
Posts: 303
Joined: Sat Jan 14, 2023 6:55 pm
Location: Bavaria/Germany

Re: Default Values in Structure Definition

Post by SMaag »

Yes, it would be a nice feature. In Pascal you can do this!
But it is same kind of feature as predfining static Arrays.

Predefinig values in the Structure defintion don't solve the problem at all.
The next feature would be a dynamic defintion, what is regulary needed.

I use a default var to implement that functionality.

Code: Select all

EnableExplicit

Structure TRect
  x.i
  y.i
  w.i
  h.i
EndStructure

Global TRect_default.TRect

With TRect_default
  \x = 10
  \y = 10
  \w = 20
  \h = 20
EndWith

Global MyRect.TRect = TRect_default

With MyRect
  Debug \x
  Debug \y
  Debug \w
  Debug \h
EndWith

or for dynamic definitions

Code: Select all


EnableExplicit

Structure TRect
  ID.i
  x.i
  y.i
  w.i
  h.i
EndStructure

Procedure TRect_default(*Rect.TRect)
  Static.i ID, x=10, y=10, w=20, h=20
  
  With *Rect
    \ID = ID
    \x = x
    \y = y
    \w = w
    \h = h
  EndWith
  
  ID +1
  x +10
  y +10
  
EndProcedure

Global MyRect1.TRect : TRect_default(MyRect1)
Global MyRect2.TRect : TRect_default(MyRect2)

With MyRect1
  Debug \ID
  Debug \x
  Debug \y
  Debug \w
  Debug \h
EndWith

With MyRect2
  Debug \ID
  Debug \x
  Debug \y
  Debug \w
  Debug \h
EndWith

or dynamic with Macro

Code: Select all


EnableExplicit

Structure TRect
  ID.i
  x.i
  y.i
  w.i
  h.i
EndStructure

Procedure TRect_default(*Rect.TRect)
  Static.i ID, x=10, y=10, w=20, h=20
  
  With *Rect
    \ID = ID
    \x = x
    \y = y
    \w = w
    \h = h
  EndWith
  
  ID +1
  x +10
  y +10
  
EndProcedure

Macro New_TRect_default(VarName)
  VarName.TRect : TRect_default(VarName)
EndMacro

;Global MyRect1.TRect : TRect_default(MyRect1)
;Global MyRect2.TRect : TRect_default(MyRect2)

Global New_TRect_default(MyRect1)
Define New_TRect_default(MyRect2)

With MyRect1
  Debug \ID
  Debug \x
  Debug \y
  Debug \w
  Debug \h
EndWith

With MyRect2
  Debug \ID
  Debug \x
  Debug \y
  Debug \w
  Debug \h
EndWith

I guess a good solution would be to add examples to the documentation how to predefine Structures in an effective way.
That will solve the problem more effective as implementing a static predefining
Post Reply