(Bug?) Exception if accessing/modifying object inside Array<Dynamic> using Accessor property

27 views
Skip to first unread message

Vsio Stitched

unread,
Sep 22, 2013, 1:15:19 PM9/22/13
to haxe...@googlegroups.com
Hello everyone,
So, when I tried to access/modify property using accessor variable of object which is inside Array<Dynamic>, it will cause exception if I access/modify it using its Accessor.
So, here's example:

Pet.hx
class Pet
{
public var _myName:String;
public function new(myName_:String) 
{
_myName = myName_;
}
public function sing():Void
{
Lib.trace(_myName +" sings.");
}
// ACCESSOR
function get_myName():String 
{
return _myName;
}
public function set_myName(value:String):String 
{
return _myName = value;
}
public var myName(get_myName, set_myName):String; // << The Accessor
}

Now, somewhere in the Main.hx
var pet_house:Array<Dynamic> = new Array<Dynamic>();

pet_house.push(new Pet("Doggy Style"));
pet_house.push(new Pet("Catty"));
pet_house.push(new Pet("Bone"));
pet_house.push(new Pet("Barky"));
pet_house.push(new Pet("9 Sucker"));
pet_house[0].sing();
Lib.trace(pet_house[1].myName); // Will cause exception if using the accessor
Lib.trace(pet_house[1]._myName); // Fine (assume if the field is set to public)

When I called "pet_house[1].myName", it will cause exception. But if I called "pet_house[1]._myName", it will be fine.

So, maybe it is a bug and should be fixed?
Thanks ^^.

Alexander Kuzmenko

unread,
Sep 22, 2013, 3:23:01 PM9/22/13
to haxe...@googlegroups.com
I also face this bug a lot when using macros in macros. And it's really annoying.
It happens only for flash target and exception message is something like "error #1069: Can't find property myName in Pet".
I think this is caused because at compile time haxe remove all properties which are not variables and replace them with pure accessors calls. However compiller does not know if dynamic object has accessor and generate a bytecode referencing absent property.
Workaround is to mark such propeties with @:isVar meta data.

воскресенье, 22 сентября 2013 г., 21:15:19 UTC+4 пользователь Vsio Stitched написал:

Luca

unread,
Sep 22, 2013, 7:13:28 PM9/22/13
to haxe...@googlegroups.com
Of course, the better solution is use Array<Pet> rather than Array<Dynamic> ...
Reply all
Reply to author
Forward
0 new messages