Structures with default values

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
GPI
PureBasic Expert
PureBasic Expert
Posts: 1394
Joined: Fri Apr 25, 2003 6:41 pm

Structures with default values

Post 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.
c4s
Addict
Addict
Posts: 1981
Joined: Thu Nov 01, 2007 5:37 pm
Location: Germany

Re: Structures with default values

Post by c4s »

+1
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: Structures with default values

Post 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?
Image
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds
GPI
PureBasic Expert
PureBasic Expert
Posts: 1394
Joined: Fri Apr 25, 2003 6:41 pm

Re: Structures with default values

Post 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()
User avatar
STARGÅTE
Addict
Addict
Posts: 2227
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Structures with default values

Post by STARGÅTE »

PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
GPI
PureBasic Expert
PureBasic Expert
Posts: 1394
Joined: Fri Apr 25, 2003 6:41 pm

Re: Structures with default values

Post by GPI »

Sometimes it is not a bad idea to rethink an old idea :)
Post Reply