Page 1 of 1

Structures with default values

Posted: Tue Mar 21, 2017 5:30 pm
by GPI
for example:

Code: Select all

Structure Counter
  value.i = 10
EndStructure

a.counter
debug a\value ;should return 10
It should be possible to add ?labels and @procedures as default values.

Re: Structures with default values

Posted: Tue Mar 21, 2017 7:53 pm
by c4s
+1

Re: Structures with default values

Posted: Tue Mar 21, 2017 8:08 pm
by Shield
I know this doesn't have the syntactic sugar, but you may be able to use this:

Code: Select all

Structure Car
  name.s
  speed.i
EndStructure

Global DefaultCar.Car
DefaultCar\name = "DefaultCar"
DefaultCar\speed = 100

Procedure DoCarStuff()
  Protected car.Car
  
  car = DefaultCar
  Debug car\name
  Debug car\speed
EndProcedure

DoCarStuff()
I'd be more for something like a default constructor function as defining the default values inline
is limiting, e.g. what if the struct contains arrays or lists/maps?

Re: Structures with default values

Posted: Tue Mar 21, 2017 8:51 pm
by GPI
In lua there are things like Metatables, which are able to change the behaviour of tables/variables.
Maybe something like CreateHook(structure, hooktype,@hock()) , wher hooktype could be #pb_hook_creation, #pb_hook_destruction and so on.

But i think it is too "anti-Basic". The solution above is easy to understand.

I would solve this then like this:

Code: Select all

Prototype __create(*structure)
Declare something_create()
Structure something
  create.__create = @something_create()
  ...
EndStructure
Procedure something_create(*self.something)
  ....
EndProcedure

Define x.something\create(x)
or when you look at my other Request:

Code: Select all

Declare something_create()
Structure something
  create.i (*structure) = @something_create()
  ...
EndStructure
Procedure something_create(*self.something)
  ....
EndProcedure

Define x.something\\create()

Re: Structures with default values

Posted: Wed Mar 22, 2017 9:45 am
by STARGĂ…TE

Re: Structures with default values

Posted: Wed Mar 22, 2017 3:17 pm
by GPI
Sometimes it is not a bad idea to rethink an old idea :)