typeof

Just starting out? Need help? Post your questions and find answers here.
User avatar
braveheart
User
User
Posts: 37
Joined: Mon Jan 04, 2010 5:54 pm

typeof

Post by braveheart »

Guys, is there a function to display type of variables like:
i.d = 2.5
Debug isDouble(i)
User avatar
idle
Always Here
Always Here
Posts: 5836
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: typeof

Post 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 

Windows 11, Manjaro, Raspberry Pi OS
Image
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: typeof

Post 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
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
DarkDragon
Addict
Addict
Posts: 2344
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Re: typeof

Post 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.
bye,
Daniel
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: typeof

Post 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
User avatar
idle
Always Here
Always Here
Posts: 5836
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: typeof

Post 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.
Windows 11, Manjaro, Raspberry Pi OS
Image
Post Reply