Anonymous structures for parameter types
Posted: Sun Jan 08, 2017 4:56 pm
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)