How to cast with class in variable?

134 views
Skip to first unread message

Théo Sabattié

unread,
May 17, 2016, 5:12:43 AM5/17/16
to Haxe
Hi All'

We can do :
cast(myInstance, MyClass);

But I want define MyClass as paramater of function or in a variable.

var lClass:Class<Dynamic> = MyClass;
cast(myInstance, lClass);

That does not work:
src/Main.hx:20: characters 27-28 : Unexpected )

Any idea?

Théo Sabattié

unread,
May 26, 2016, 5:59:37 AM5/26/16
to Haxe
It is for pixijs, I want to add a getter which return the researched child using generic type

    private function getChild<T>(childName:String):T {
        var child:DisplayObject = getChildByName(childName);
        
        if (child == null) {
           throw "Child with name " + childName + " not found";
        }
        
        if (!Std.is(child, T)) {
            throw "Child with name " + childName + " is not an instance of " + Type.getClassName(T);
        }
        
        return cast(child, T);
    }

I have those errors:
src/UIComponent.hx:79: characters 22-23 : Type parameter T is only available at compilation and is not a runtime value
src/UIComponent.hx:85: characters 27-28 : Type parameter T is only available at compilation and is not a runtime value
src/UIComponent.hx:86: characters 97-98 : Type parameter T is only available at compilation and is not a runtime value
src/UIComponent.hx:89: characters 15-29 : Can't cast to a type parameter
src/UIComponent.hx:76: lines 76-90 : Missing return getChild.T
Build halted with errors (haxe.exe).

How can I do test and cast?

clemos

unread,
May 26, 2016, 6:04:06 AM5/26/16
to haxe...@googlegroups.com
I think you can just `return cast child;`, which the compiler will
infer to whatever T is.
> --
> To post to this group haxe...@googlegroups.com
> http://groups.google.com/group/haxelang?hl=en
> ---
> You received this message because you are subscribed to the Google Groups
> "Haxe" group.
> For more options, visit https://groups.google.com/d/optout.

Théo Sabattié

unread,
May 31, 2016, 4:50:44 AM5/31/16
to Haxe
I can do that but I have to check if displayObject is an instance of T.
And 

Std.is(child, T)

generate:

src/UIComponent.hx:79: characters 22-23 : Type parameter T is only available
> at compilation and is not a runtime value




clemos

unread,
May 31, 2016, 5:06:04 AM5/31/16
to haxe...@googlegroups.com
Yeah you can't do that, you have to pass the actual class at runtime,
something like:
private function getChild<T>(childName:String, type: Class<T>): T {
//...
if( !Std.is( child, type ) {
//...
}
//...
}

On Tue, May 31, 2016 at 10:50 AM, Théo Sabattié

Théo Sabattié

unread,
May 31, 2016, 5:34:39 AM5/31/16
to Haxe
Great! It's running! :)

Thanks clemos.

thought use 
getChild<Graphics>("_gridZone", Graphics);

But it is running with

getChild("_gridZone", Graphics);

I have juste a last problem:

private function getChild<T>(childName:String, type:Class<T>):T {
        var child:DisplayObject = getChildByName(childName);
        
        if (child == null) {
           throw "Child with name " + childName + " not found";
        }
        
        if (!Std.is(child, type)) {
            throw "Child with name " + childName + " is not an instance of " + Type.getClassName(type);
        }
        
        return cast child;
    }

Type.getClassName(type)

return null


I can't inform user.

Reply all
Reply to author
Forward
0 new messages