Anonymous structures for parameter types

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Anonymous structures for parameter types

Post by Mistrel »

We don't have templates but perhaps we can have something similar with anonymous structures for parameter types. These types would allow the passing and accessing of structures which contain only a single field. This works well with PureBasic's built-in structures for Integer, Word, Double, etc:

Code: Select all

;/ Anonymous structures for parameter types
Procedure.i Compare(*a.?, *b.?)
  If *a\? > *b\?
    ProcedureReturn 1
  EndIf
  
  If *a\? < *b\?
    ProcedureReturn -1
  EndIf
  
  ProcedureReturn 0
EndProcedure

a.Integer\i=1
b.Integer\i=2

;/ Output: -1
Debug Compare(@a,@b)

c.i=3
d.i=2

;/ Variables can be copy-assigned anonymously to structures with '->'
;/ Output: 1
Debug Compare(c->Integer,d->Integer)

;/ Structures of the same type can be copy-assigned as well
;/ Output: 0
Debug Compare(a->Integer,a->Integer)

;/ Literals are automatically promoted. Here, 1->Integer and 2.0->Double
Debug Compare(1,2.0)

;/ Literals as 1->Integer and 2.0->Float
Debug Compare(1,2.0->Float)
User avatar
Lunasole
Addict
Addict
Posts: 1091
Joined: Mon Oct 26, 2015 2:55 am
Location: UA
Contact:

Re: Anonymous structures for parameter types

Post by Lunasole »

Mistrel wrote:We don't have templates but perhaps we can have something similar with anonymous structures for parameter types.
If I've got right what you talked about, you already can do it using following or similar "low-level access"/pointer arithmetic:

Code: Select all

;/ Anonymous structures for parameter types
Procedure.i Compare(*a, *b)
  If PeekI(*a + OffsetOf(Integer\i)) > PeekI(*b + OffsetOf(Integer\i))
    ProcedureReturn 1
  EndIf
  
  If PeekI(*a + OffsetOf(Integer\i)) < PeekI(*b + OffsetOf(Integer\i))
    ProcedureReturn -1
  EndIf
  
  ProcedureReturn 0
EndProcedure

a.Integer\i=1
b.Integer\i=2

;/ Output: -1
Debug Compare(@a,@b)
;/ Output: 1
Debug Compare(@b,@a)

Also anyway what you proposed looks impossible to be added, as it requires much more complex compiler (which requires lot of efforts, C++ compilers doing that stuff with -> are so smart [and so slow] because whole army of ppl worked on them for many years [and added lot of trash stuff, btw ^^], PB is done another way).
"W̷i̷s̷h̷i̷n̷g o̷n a s̷t̷a̷r"
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Re: Anonymous structures for parameter types

Post by Mistrel »

Your example is not the same as the first field for Integer is '\i' whereas the field for Float is '\f'.
User avatar
Lunasole
Addict
Addict
Posts: 1091
Joined: Mon Oct 26, 2015 2:55 am
Location: UA
Contact:

Re: Anonymous structures for parameter types

Post by Lunasole »

Mistrel wrote:Your example is not the same as the first field for Integer is '\i' whereas the field for Float is '\f'.
That was maximally simplified example. It doesn't matter which "type" or field to use [\i and \f offsets and sizes are the same], you can store your Float inside of Integer variable [for example] using PokeF(), or special structure with StructureUnion representing variant type, etc.

Generally all what you need can be done already manually/using few macro. I'm just sad that "class modules" can't be implemented so easy as your stuff ^^
"W̷i̷s̷h̷i̷n̷g o̷n a s̷t̷a̷r"
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Re: Anonymous structures for parameter types

Post by Mistrel »

PokeI() is not the same as PokeF(). You have to hard-code one of them.
User avatar
Lunasole
Addict
Addict
Posts: 1091
Joined: Mon Oct 26, 2015 2:55 am
Location: UA
Contact:

Re: Anonymous structures for parameter types

Post by Lunasole »

Mistrel wrote:PokeI() is not the same as PokeF(). You have to hard-code one of them.
Anyway, what is the problem then to use Float/Double only, also for integer representation? That's how such trash is implemented in Javascript.
"W̷i̷s̷h̷i̷n̷g o̷n a s̷t̷a̷r"
Post Reply