Finding out what 'type' a variable is

BlitzMax Forums/BlitzMax Beginners Area/Finding out what 'type' a variable is

EOF(Posted 2009) [#1]
This does not work but is pseudo code. How can I check what type has been supplied to the WhatIs function?
Do I need to do it via Reflection?


Strict
Framework brl.standardio

Local b:Int[]=[6,7,8]
WhatIs b
End

Function WhatIs(o:Object)
	If Int(o) Then Print "INT"
	If Float(o) Then Print "FLOAT"
	If IntArray(o) Then Print "INT ARRAY"
End Function



N(Posted 2009) [#2]
Function IsKindOf(obj:Object, typ:TTypeID)
    Return TTypeID.ForObject(obj).ExtendsType(typ)
End Function
Also
TTypeID.ForObject(obj)



EOF(Posted 2009) [#3]
Mmm .. I think I might be going about this the wrong way?


Here, querying variable b produces nothing
Variable a cannot be converted into an object

What seems to be needed is 'Any' for the WhatIs() variable(??)

Strict
Framework brl.standardio
Import brl.reflection

Local b:Int[]=[6,7,8]
WhatIs b
Local a:Int=6
'WhatIs a
End

Function WhatIs(o:Object)
	Local t:TTypeId=TTypeId.ForObject(o)
	If t=IntTypeId Print "int"
	If t=ArrayTypeId Print "array"
	If t=ObjectTypeId Print "object"
End Function



plash(Posted 2009) [#4]
I would've expected the type id for the array would show as ArrayTypeId, but it does not..

Also, data types (except for String) are not objects.
SuperStrict

Framework BRL.StandardIO
Import BRL.Reflection

Local b:Int[] = [6, 7, 8]
WhatIs(b)

Local a:Int = 6
'WhatIs(a)	' This simply will not work, as no data types are actually objects (except for String)
End

Function WhatIs(obj:Object)
	Local id:TTypeId = TTypeId.ForObject(obj)
	Print("Object type: " + id.Name())
End Function



N(Posted 2009) [#5]
ArrayTypeID is a sort of useless, generic on its own - the easiest way to check if a returned TTypeID is an array type is to compare their classes:
If TTypeID.ForObject(obj)._class = ArrayTypeID._class Then ...