Reflection: get list of derived classes?

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

Reflection: get list of derived classes?

slenkar
If I had a class called Car
and several classes like 'Ford' 'Honda' that Extend Car

Could I get a list of the classes that extend Car using reflection?
Reply | Threaded
Open this post in threaded view
|

Re: Reflection: get list of derived classes?

Franco Ponticelli
Short answer: no, you cannot.
But you can keep a registry of those classes and use a static initializer to automatically subscribe the imported classes.

Franco

On Fri, Nov 12, 2010 at 9:00 PM, slenkar <[hidden email]> wrote:

If I had a class called Car
and several classes like 'Ford' 'Honda' that Extend Car

Could I get a list of the classes that extend Car using reflection?
--
View this message in context: http://haxe.1354130.n2.nabble.com/Reflection-get-list-of-derived-classes-tp5733978p5733978.html
Sent from the Haxe mailing list archive at Nabble.com.

--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Reflection: get list of derived classes?

slenkar
thanks for the reply,

how would I subscribe the imported classes?

Reply | Threaded
Open this post in threaded view
|

Re: Reflection: get list of derived classes?

jlm@justinfront.net
When constructing you pass the instance to a static ( class ) method  
on the base class that then add it to a static ( class variable ) list.
Later you can iterate through the list and check class types.

I have used it in AS3 for making sure that I stop other video  
instances when playing... I assume similar in haxe so you just pass  
the instance in the super( this );

then in the base class constructor

MyBase.addToInheritedList( _thisInstance );

There maybe other ways, and you may not have to reference MyBase to  
get to the static method.


On 13 Nov 2010, at 01:20, slenkar wrote:

>
> thanks for the reply,
>
> how would I subscribe the imported classes?
>
>
> --
> View this message in context: http://haxe.1354130.n2.nabble.com/Reflection-get-list-of-derived-classes-tp5733978p5734517.html
> Sent from the Haxe mailing list archive at Nabble.com.
>
> --
> haXe - an open source web programming language
> http://haxe.org


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Reflection: get list of derived classes?

jlm@justinfront.net
I would consider it a bit hacky from an oop perspective, but it should  
work and that's sometimes more important.

On 13 Nov 2010, at 01:54, [hidden email] wrote:

> When constructing you pass the instance to a static ( class ) method  
> on the base class that then add it to a static ( class variable )  
> list.
> Later you can iterate through the list and check class types.
>
> I have used it in AS3 for making sure that I stop other video  
> instances when playing... I assume similar in haxe so you just pass  
> the instance in the super( this );
>
> then in the base class constructor
>
> MyBase.addToInheritedList( _thisInstance );
>
> There maybe other ways, and you may not have to reference MyBase to  
> get to the static method.
>
>
> On 13 Nov 2010, at 01:20, slenkar wrote:
>
>>
>> thanks for the reply,
>>
>> how would I subscribe the imported classes?
>>
>>
>> --
>> View this message in context: http://haxe.1354130.n2.nabble.com/Reflection-get-list-of-derived-classes-tp5733978p5734517.html
>> Sent from the Haxe mailing list archive at Nabble.com.
>>
>> --
>> haXe - an open source web programming language
>> http://haxe.org
>
>
> --
> haXe - an open source web programming language
> http://haxe.org


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Reflection: get list of derived classes?

jlm@justinfront.net
Let me know if you need an example.

On 13 Nov 2010, at 01:57, [hidden email] wrote:

> I would consider it a bit hacky from an oop perspective, but it  
> should work and that's sometimes more important.
>
> On 13 Nov 2010, at 01:54, [hidden email] wrote:
>
>> When constructing you pass the instance to a static ( class )  
>> method on the base class that then add it to a static ( class  
>> variable ) list.
>> Later you can iterate through the list and check class types.
>>
>> I have used it in AS3 for making sure that I stop other video  
>> instances when playing... I assume similar in haxe so you just pass  
>> the instance in the super( this );
>>
>> then in the base class constructor
>>
>> MyBase.addToInheritedList( _thisInstance );
>>
>> There maybe other ways, and you may not have to reference MyBase to  
>> get to the static method.
>>
>>
>> On 13 Nov 2010, at 01:20, slenkar wrote:
>>
>>>
>>> thanks for the reply,
>>>
>>> how would I subscribe the imported classes?
>>>
>>>
>>> --
>>> View this message in context: http://haxe.1354130.n2.nabble.com/Reflection-get-list-of-derived-classes-tp5733978p5734517.html
>>> Sent from the Haxe mailing list archive at Nabble.com.
>>>
>>> --
>>> haXe - an open source web programming language
>>> http://haxe.org
>>
>>
>> --
>> haXe - an open source web programming language
>> http://haxe.org
>
>
> --
> haXe - an open source web programming language
> http://haxe.org


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Reflection: get list of derived classes?

jlm@justinfront.net
In reply to this post by jlm@justinfront.net
actually you don't need to pass this instance my mistake.

you just need something like this in the base class

function new()
{
MyBase.addToInheritedList( this );
}

you don't have to use an access method but I think it should allow  
your list to be a private static.


On 13 Nov 2010, at 01:54, [hidden email] wrote:

> When constructing you pass the instance to a static ( class ) method  
> on the base class that then add it to a static ( class variable )  
> list.
> Later you can iterate through the list and check class types.
>
> I have used it in AS3 for making sure that I stop other video  
> instances when playing... I assume similar in haxe so you just pass  
> the instance in the super( this );
>
> then in the base class constructor
>
> MyBase.addToInheritedList( _thisInstance );
>
> There maybe other ways, and you may not have to reference MyBase to  
> get to the static method.
>
>
> On 13 Nov 2010, at 01:20, slenkar wrote:
>
>>
>> thanks for the reply,
>>
>> how would I subscribe the imported classes?
>>
>>
>> --
>> View this message in context: http://haxe.1354130.n2.nabble.com/Reflection-get-list-of-derived-classes-tp5733978p5734517.html
>> Sent from the Haxe mailing list archive at Nabble.com.
>>
>> --
>> haXe - an open source web programming language
>> http://haxe.org
>
>
> --
> haXe - an open source web programming language
> http://haxe.org


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Reflection: get list of derived classes?

slenkar
going further with the car example

lets say I have the classes Honda and Ford (and others Chevy,Nissan etc.)

then I want to print the names to the screen so the player can choose which car to drive

so I need a list of class names before any new objects are created

then when the player clicks on a button
I want to use the text from the button to create a new instance of the right object (Ford or Honda)
Reply | Threaded
Open this post in threaded view
|

Re: Reflection: get list of derived classes?

jlm@justinfront.net
The best thing is to just put the classes in a list or array

var cars: Array<Class> = [Honda,Ford,Chevy,Nissan];

then you can use reflection to get the names as strings, otherwise you  
need to be super smart and try to somehow use http://haxe.org/api/haxe/rtti/infos

in flash you can maybe get hold of some XML that stores info as well,  
but I think your trying to hard to be smart with code, it's just  
easier to keep it simple and even just create an array of strings.

You know in flash unless you have used the class it won't import it,  
not sure if its exactly same in flash haXe but for parsing openpyro  
( hx and as3 versions ) from XML I have been constructing object/
hashes of classes based on there names as it's the easiest way to  
dynamically be able to construct them from the XML.

;j


On 13 Nov 2010, at 19:15, slenkar wrote:

>
> going further with the car example
>
> lets say I have the classes Honda and Ford (and others Chevy,Nissan  
> etc.)
>
> then I want to print the names to the screen so the player can  
> choose which
> car to drive
>
> so I need a list of class names before any new objects are created
>
> then when the player clicks on a button
> I want to use the text from the button to create a new instance of  
> the right
> object (Ford or Honda)
> --
> View this message in context: http://haxe.1354130.n2.nabble.com/Reflection-get-list-of-derived-classes-tp5733978p5735966.html
> Sent from the Haxe mailing list archive at Nabble.com.
>
> --
> haXe - an open source web programming language
> http://haxe.org


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Reflection: get list of derived classes?

jlm@justinfront.net
might need full class paths.
On 13 Nov 2010, at 19:48, [hidden email] wrote:

> The best thing is to just put the classes in a list or array
>
> var cars: Array<Class> = [Honda,Ford,Chevy,Nissan];
>
> then you can use reflection to get the names as strings, otherwise  
> you need to be super smart and try to somehow use http://haxe.org/api/haxe/rtti/infos
>
> in flash you can maybe get hold of some XML that stores info as  
> well, but I think your trying to hard to be smart with code, it's  
> just easier to keep it simple and even just create an array of  
> strings.
>
> You know in flash unless you have used the class it won't import it,  
> not sure if its exactly same in flash haXe but for parsing openpyro  
> ( hx and as3 versions ) from XML I have been constructing object/
> hashes of classes based on there names as it's the easiest way to  
> dynamically be able to construct them from the XML.
>
> ;j
>
>
> On 13 Nov 2010, at 19:15, slenkar wrote:
>
>>
>> going further with the car example
>>
>> lets say I have the classes Honda and Ford (and others Chevy,Nissan  
>> etc.)
>>
>> then I want to print the names to the screen so the player can  
>> choose which
>> car to drive
>>
>> so I need a list of class names before any new objects are created
>>
>> then when the player clicks on a button
>> I want to use the text from the button to create a new instance of  
>> the right
>> object (Ford or Honda)
>> --
>> View this message in context: http://haxe.1354130.n2.nabble.com/Reflection-get-list-of-derived-classes-tp5733978p5735966.html
>> Sent from the Haxe mailing list archive at Nabble.com.
>>
>> --
>> haXe - an open source web programming language
>> http://haxe.org
>
>
> --
> haXe - an open source web programming language
> http://haxe.org


--
haXe - an open source web programming language
http://haxe.org