Page 1 of 1

Anonymous structures for parameter types

Posted: Sun Jan 08, 2017 4:56 pm
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)

Re: Anonymous structures for parameter types

Posted: Mon Jan 09, 2017 11:23 pm
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).

Re: Anonymous structures for parameter types

Posted: Tue Jan 10, 2017 4:17 am
by Mistrel
Your example is not the same as the first field for Integer is '\i' whereas the field for Float is '\f'.

Re: Anonymous structures for parameter types

Posted: Tue Jan 10, 2017 4:57 am
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 ^^

Re: Anonymous structures for parameter types

Posted: Tue Jan 10, 2017 5:12 am
by Mistrel
PokeI() is not the same as PokeF(). You have to hard-code one of them.

Re: Anonymous structures for parameter types

Posted: Tue Jan 10, 2017 5:23 am
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.