Bus error with simple reflection example

Monkey Forums/Monkey Bug Reports/Bus error with simple reflection example

fictorial(Posted 2013) [#1]
Import reflection

Class C
End

Function Main()
	Local c:=New C
	Print GetClass( c ).Name
End


On build and run, I get a "Bus error" with the following Return statement highlighted in reflection.monkey:

Function GetClass:ClassInfo( obj:Object )
	Return _getClass.GetClass( obj )
End


GLFW target; Mac OS X 10.7.5; Monkey v68b


marksibly(Posted 2013) [#2]
You need to set a reflection filter, eg:

#REFLECTION_FILTER="*"

...but it should do this for you, will fix.


fictorial(Posted 2013) [#3]
OK that worked. How do I get the type of a primitive like Int?

#REFLECTION_FILTER="*"

Import reflection

Class C
End

Function Main()
	Local c:Int = 52
	Print GetClass( c ).Name
End



fictorial(Posted 2013) [#4]
Getting a memory access error when running the above using GLFW Game target:

http://imgur.com/w4i4yGs

OS X 10.7; Monkey 68c

I updated the GLFW template XCode project to use the 10.7 SDK.

EDIT - the screenshot shows an error; updated to the following code to this and it works fine:

Local c:C = new C


But, this fails with the memory access error:

Local c := 42


EDIT2 - OK, so I suppose Int is not an object so this might be expected. So I am back to my original question: how do I get type info for any type?


fictorial(Posted 2013) [#5]
Here is one answer to my own question.

[monkeycode]
#REFLECTION_FILTER="*"

Import reflection

Function GetType$(o:Object) Return GetClass(o).Name End
Function GetType$(i%) Return "Int" End
Function GetType$(f#) Return "Float" End
Function GetType$(s$) Return "String" End
Function GetType$(b?) Return "Bool" End
Function GetType$(a[]) Return "Array" End

Class C
End

Function Main()
Print GetType(42)
Print GetType(3.14)
Print GetType("Foo")
Print GetType(True)
Print GetType([1,2,3])
Print GetType(New C)
End
[/monkeycode]


marksibly(Posted 2013) [#6]
> How do I get the type of a primitive like Int?

You can't - ints, floats, strings aren't object's (ie: they don't have any runtime info attached) so can't be 'interrogated' at runtime for their type.