Page 1 of 1

typeof

Posted: Sat Jul 14, 2012 5:06 am
by braveheart
Guys, is there a function to display type of variables like:
i.d = 2.5
Debug isDouble(i)

Re: typeof

Posted: Sat Jul 14, 2012 5:50 am
by idle
no you would need to make use of opaque types
for example something like this

Code: Select all

Structure opaque 
  type.i
  StructureUnion
    a.a
    b.b
    u.u
    c.c
    w.w
    l.l
    i.i
    ptr.i
    f.f
    d.d
    q.q
  EndStructureUnion 
  s.s 
EndStructure  

Define var.opaque 
var\type = #PB_Double 
var\d = #PI 

If var\type = #PB_Double 
Debug StrD(var\d,14) 
EndIf 


Re: typeof

Posted: Sat Jul 14, 2012 6:22 am
by IdeasVacuum
....just prefix the name of your vars and you'll always know what they are:

iMyInt.i
dMyDouble.d
sMyString.s

I differentiate between Global Vars and Local vars by giving the Global a 'g' prefix:

igMyInt.i
dgMyDouble.d
sgMyString.s

etc

Re: typeof

Posted: Sat Jul 14, 2012 8:38 am
by DarkDragon
You'd need reflection or at least type introspection for this. I myself thought about a precompiler for purebasic to do this, but I have no time. Maybe someone who did one of those OOP precompilers could do it.

Re: typeof

Posted: Sat Jul 14, 2012 8:47 am
by Danilo
DarkDragon wrote:You'd need reflection for this. I myself thought about a precompiler for purebasic to do this, but I have no time. Maybe someone who did one of those OOP precompilers could do it.
Reflection is also used to get functions and types from outside (of a DLL/EXE).

The PB compiler knows the variable type already, so typeof(variable) could be easily added and
it could be useful together with macros. Maybe a new feature request?

Code: Select all

Macro ToString(_value_)
    CompilerSelect TypeOf(_value_)
        CompilerCase #PB_TYPE_FLOAT
            ( StrF(_value_) )
        CompilerCase  #PB_TYPE_DOUBLE
            ( StrD(_value_) )
        CompilerDefault
            ( Str( _value_ ) )
    CompilerEndSelect
EndMacro

Re: typeof

Posted: Sat Jul 14, 2012 10:49 am
by idle
yes I think a compiler directive could be useful, but if you want to know what a type is at runtime
you either need to use a runtime component to allocate your variables on the heap which also tracks the types
or use opaque types, if that's a fair use of the term.