Author Topic: any  (Read 4127 times)

0 Members and 1 Guest are viewing this topic.

pber

  • Guest
any
« 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?

JRS

  • Guest
Re: any
« Reply #1 on: July 02, 2014, 04:45:07 PM »
I always thought any meant a pointer with unknown type. It is up to the programmer to deal with.


pber

  • Guest
Re: any
« Reply #2 on: July 02, 2014, 05:13:56 PM »
you was not wrong

JRS

  • Guest
Re: any
« Reply #3 on: July 02, 2014, 05:17:05 PM »
I treat any like a type union. Just treat the any pointer as a string if you know that is what it points to.


pber

  • Guest
Re: any
« Reply #4 on: July 02, 2014, 05:20:51 PM »
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.

JRS

  • Guest
Re: any
« Reply #5 on: July 02, 2014, 06:28:58 PM »
I wouldn't know anything about that.  :o

Charles Pegge

  • Guest
Re: any
« Reply #6 on: July 02, 2014, 08:45:16 PM »
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.

Code: [Select]
'==========================
'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.

Code: [Select]
'==========================
'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'

Code: [Select]
'==========================
'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
« Last Edit: July 02, 2014, 09:28:26 PM by Charles Pegge »

pber

  • Guest
Re: any
« Reply #7 on: July 03, 2014, 04:41:13 AM »
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:
Code: [Select]
=@retval ::)

Charles Pegge

  • Guest
Re: any
« Reply #8 on: July 04, 2014, 12:19:08 AM »

You will run into various problems attempting to return any, but you can return values using byref parameters, the same way as passing them.


Charles Pegge

  • Guest
Re: any
« Reply #9 on: July 07, 2014, 02:49:29 AM »

Returning pointers can be useful.

Here's a simple example, pre-calcualting a table converting degrees to sines

Code: [Select]
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


pber

  • Guest
Re: any
« Reply #10 on: July 07, 2014, 05:16:22 AM »
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.

Charles Pegge

  • Guest
Re: any
« Reply #11 on: July 07, 2014, 06:05:36 AM »
Hi Paolo,

These are equivalent:

float sind at fsindeg()

float *sind
@sind=fsindeg()

float *sind=fsindeg()


pber

  • Guest
Re: any
« Reply #12 on: July 07, 2014, 06:20:03 AM »
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...


pber

  • Guest
Re: any
« Reply #13 on: July 07, 2014, 06:34:11 AM »
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?

pber

  • Guest
Re: any
« Reply #14 on: July 07, 2014, 06:46:51 AM »
...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.