Page 2 of 2

Re: Default Values in Structure Definition

Posted: Tue Jun 11, 2019 10:05 pm
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.

Re: Default Values in Structure Definition

Posted: Wed Jun 12, 2019 1:50 am
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.

Re: Default Values in Structure Definition

Posted: Wed Jun 12, 2019 2:13 am
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

Re: Default Values in Structure Definition

Posted: Thu Jun 13, 2019 3:35 pm
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

Re: Default Values in Structure Definition

Posted: Mon Jun 17, 2019 12:29 pm
by diceman
That would be a nifty thing, indeed! 8)

Re: Default Values in Structure Definition

Posted: Sat Jun 07, 2025 12:47 pm
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.

Re: Default Values in Structure Definition

Posted: Sat Jun 07, 2025 2:14 pm
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.

Re: Default Values in Structure Definition

Posted: Sat Jun 07, 2025 4:06 pm
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.

Re: Default Values in Structure Definition

Posted: Sat Jun 07, 2025 4:56 pm
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.

Re: Default Values in Structure Definition

Posted: Sat Jun 07, 2025 5:54 pm
by mk-soft
NumericMap would be fine. Since there is already internal at PB.

Re: Default Values in Structure Definition

Posted: Sat Jun 07, 2025 5:57 pm
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: )

Re: Default Values in Structure Definition

Posted: Sat Jun 07, 2025 10:22 pm
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