Oxygen Basic
Programming => Problems & Solutions => Topic started by: pber on July 02, 2014, 04:20:08 PM
-
in TypeAware1 I see a way to pass a string through a param as any.
Does exist a way to return a string from a function declared as any?
-
I always thought any meant a pointer with unknown type. It is up to the programmer to deal with.
-
you was not wrong
-
I treat any like a type union. Just treat the any pointer as a string if you know that is what it points to.
-
simply I don't find a way to retrieve the string.
It should not be so difficult... in fact I got anything
(including crashes) but the string sent.
-
I wouldn't know anything about that. :o
-
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
-
Thanks Charles, we are in debt to you.
Apologize to me (John too), I did some confusion,
I was trying to return from a function as any
that in turns return from a dynamically compiled code.
What I missed was the "@" in:
=@retval
::)
-
You will run into various problems attempting to return any, but you can return values using byref parameters, the same way as passing them.
-
Returning pointers can be useful.
Here's a simple example, pre-calcualting a table converting degrees to sines
indexbase 0
function fsindeg() as sys
=========================
float s at getmemory 360 * sizeof float
for i=0 to 359 : s[i]=sin(rad(i)) : next
return @s
end function
float sind at fsindeg()
print sind[30] '.50
...
freememory @sind
-
Hi Charles,
mmmh... now I understand your at a little bit better.
I though it was allowed only at declaration, now I see it's valid
in every time.
An elegant construct.
Anyway I'll follow your suggestion of using [in] params to return values.
-
Hi Paolo,
These are equivalent:
float sind at fsindeg()
float *sind
@sind=fsindeg()
float *sind=fsindeg()
-
That's right.
I'm still facing the issue of releasing ALL memory at runtime
(i.e.: before the termination of the program).
I find this construct, in you examples:
cast bstring @c=""
as a way of releasing allocated memory pointed by c.
c could be an array of objects.
If I understand you are delegating at the program termination the task of effectively releasing memory.
What I tried to do is casting c to a bstring and using frees to release its memory.
Sgrunt... I still have not found the syntax...
-
Charles: has this some meaning?
My test is a loop that creates an object
and then release it.
To release it should be sufficient
del obj
and I get an amount of memory used.
If I add the pointless
freememory obj
(just after del obj)
it seems the program stops with a greater amount of memory used.
To allocate and destroy obj I just used the macros new and del,
so I expect the freememory was already sent.
Trying to freememory the NULL requires memory?
-
...forget last 2 msg
freememory after del was effectively pointless,
the problem was elsewhere.
Now I can release all memory allocated for my objects.
Only now I realize that compiling dynamically
has a very cheap cost in memory
but a considerable cost in time.