[Bug?] SWF assets - instance names NULL

So this has to be a bug? …
When listening for events on instances added from within a SWF, the instance names are NULL.

Consider this code:

addEventListener(Event.ADDED, onAdded);
var mc = new MainContainer();
addChild(mc);

private function onAdded(e:Event)
{
trace(e.target);
trace(e.target.name);
}

Now this gives the following output…

Test1.hx:127: [object Sprite]
Test1.hx:128: null

The target.name property is null, but in the SWF assets this is not true…
This is not correct, the instancename should be readable…

[EDIT]

var sp = e.target;
trace(sp.name);
// NULL

var sp:Sprite = e.target;
trace(sp.name);
// MySprite1

So the last solution works… but a find this a bit odd… anyone want to share their thoughts on this?

1 Like

Looks like name uses a getter and setter. Let me check to be sure… yep, it does.

In Haxe, getters are a compile-time feature. When the compiler sees sp.name, it’ll replace it with sp.get_name(). At runtime, sp.name doesn’t exist, but that’s usually ok.

The problem is, the compiler can only do the replacement if it knows what sp is. If it doesn’t know sp is a sprite, it can’t look up the variable declaration, so it can’t tell if name uses a getter, and it leaves it as-is.

You may be used to type inference handling this for you. For instance, if you write var sp = new Sprite(), Haxe infers that sp is a sprite, and all the getters and setters will just work. However, e.target is Dynamic, so Haxe can’t infer anything when you write var sp = e.target.