Hi Paolo,
TypeAware1 had some obsolete syntax, so I have reworked it here.
OxygenBasic now auto-converts between strings and numeric types, so the code is somewhat redundant, but may be useful for handling customised types.
'==========================
'TYPE AWARENESS AND CASTING
'==========================
function f(any*v, string typ)
'============================
'
'IDENTIFY AND CAST THE VARIABLE TYPE
'
string vs
sys a
'
if typ="long" then
vs=(long) v
elseif typ="double"
vs=(double) v
elseif typ="string" then
vs=(string) @v '@' DYNAMIC STRING ODDITY
elseif typ="bstring" then
vs=(bstring) v
else
print "Unsupported type: " typ
exit function
end if
'
print typ " " vs
'
end function
'======
'TESTS:
'======
long a=12345
double d=1234.5
string s="Hello!"
bstring b="GoodBye"
f a,typeof a
f d,typeof d
f s,typeof s
f b,typeof b
frees b
And this variation returns a string.
'==========================
'TYPE AWARENESS AND CASTING
'==========================
function f(any*v, string typ) as string
'======================================
'
'IDENTIFY AND CAST THE VARIABLE TYPE
'
string vs
'
if typ="long" then
vs=(long) v
elseif typ="double"
vs=(double) v
elseif typ="string" then
vs=(string) @v '@' DYNAMIC STRING ODDITY
elseif typ="bstring" then
vs=(bstring) v
else
print "Unsupported type: " typ
exit function
end if
'
return vs
'
end function
'======
'TESTS:
'======
long a=12345
double d=1234.5
string s="Hello!"
bstring b="GoodBye"
print f a,typeof a
print f d,typeof d
print f s,typeof s
print f b,typeof b
frees b
And this one includes a customised type 'node'
'==========================
'TYPE AWARENESS AND CASTING
'==========================
type node string name, float x,y,z
function f(any*v, string typ) as string
'======================================
'
'IDENTIFY AND CAST THE VARIABLE TYPE
'
string vs
'
if typ="long" then
vs=(long) v
elseif typ="double"
vs=(double) v
elseif typ="bstring" then
vs=(bstring) v
elseif typ="string" then
vs=(string) @v '@' DYNAMIC STRING
elseif typ="node" then
node n at @v '@' HIGHER TYPES
return n.name ": " n.x "," n.y "," n.z
else
return "Unsupported type: " typ
exit function
end if
'
return vs
'
end function
'TESTS:
'======
long a=12345
double d=1234.5
string s="Hello!"
bstring b="GoodBye"
node n={"Here!", 1.5,2.5,3.5}
print f a,typeof a
print f d,typeof d
print f s,typeof s
print f b,typeof b
print f n,typeof n
frees b