reflection - what is it?

BlitzMax Forums/BlitzMax Beginners Area/reflection - what is it?

GfK(Posted 2011) [#1]
...and what can it be used for?


matibee(Posted 2011) [#2]
I think it's the same as RTTI (run time type identification)..

http://en.wikipedia.org/wiki/Run-time_type_information


jsp(Posted 2011) [#3]
Without casting to the type an instance belongs to, you can work with it. Very useful.
Adds sometimes also an extra abstraction to your code, which can be helpful as well.


GfK(Posted 2011) [#4]
matibee: C++ is miles over my head - I don't understand.

jsp: Can you give me a short example of how that works and in what circumstances it might be useful?


ziggy(Posted 2011) [#5]
It allows you to know the type name of a variable (to say), get a list of the methods it has, call them "by name". Also, get the list of available types, etc etc. I don't know very well how to explain it, as it allows you to do lots of interesting things.

As an example (not useful, but you'll get the idea), you could read from an external document a list of type names, and generate instances for them at runtime using reflection. With reflection, you don't need to know the variable types at compile time.

It's very useful for object serialization, among lots of other things.

I use it sometimes, on other languages, to provide "plugin" architectures.

EDIT: Another usage (as I said, serialization / deserialization)
you could write a function that gets an object of any kind as a parameter, and you write the type name and its field values to a document. Later, on aonther run, you can read the document back and recreate the same object, with the same data. Reflection + XML are very common.

Last edited 2011


GfK(Posted 2011) [#6]
Ah, that's starting to make a bit of sense - thanks.


jsp(Posted 2011) [#7]
A ziggy said it's difficult to explain, here a short example:

'for your new type, you don't know which one and you don't care
Local NewType:Object
'get the type id
Local MyType:TTypeId =TTypeId.ForName( "Name of type read fom an ini or xml file" )
'create whatever what was found
NewType:Object =MyType.NewObject()
'run methods of the type via the basetype, but you could also run certain methods via reflection
BaseType( NewType ).Initialize( Parameter )


GfK(Posted 2011) [#8]
I kind of get it now, thanks!


Czar Flavius(Posted 2011) [#9]
Basically you can get an "Object", which could be anything, and ask it to give you a list of methods and fields it has. For example, you could display these to the screen and a user could select one and you execute it. In game debugger?


Kryzon(Posted 2011) [#10]
I just noticed there is a small article on it in the BlitzMax help. Go to your IDE, right sidebar (the one with the documentation stuff): Modules -> BASIC -> Reflection.

Has some good words on it that might help.

Last edited 2011


Htbaa(Posted 2011) [#11]
You should not use reflection to find out if you've got an object of a certain type so you can call methods on it. Use typecasting for that.

Lugi.mod also uses reflection to find out which types and methods etc. to 'export' to Lua.


Wiebo(Posted 2011) [#12]
You can check out Brucey's MaxUnit module to see it in action in a relative simple manner. In it, you mark methods with the {test} metadata and the program can find and call the test methods in run-time. It's really cool.


Wiebo(Posted 2011) [#13]
You can check out Brucey's MaxUnit module to see it in action in a relative simple manner. In it, you mark methods with the {test} metadata and the program can find and call the test methods in run-time. It's really cool.

[edit: double cool because of the double post. sorry]

Last edited 2011