Haxe CreateInstance and properties

I want to deserialize an object with properties. The object is well built, but the setters / getters are not set correctly unless I explicitly find my object.

Is this the alleged behavior or error?

Minimal example:

package models;
class TestClass
{
    public var test(default, set): String;
    public function set_test(myVar) {
        trace("Set " + myVar);
        return test = myVar;
    }
}

class Main
{
    public function new() 
    {
       var typedTest: TestClass = Type.createInstance(Type.resolveClass("models.TestClass"), []);
       var untypedTest = Type.createInstance(Type.resolveClass("models.TestClass"), []);
       trace(Type.getClassName(Type.getClass(typedTest)));   //"models.TestClass"
       trace(Type.getClassName(Type.getClass(untypedTest )));  //"models.TestClass"
       typedTest.test = "12";  // "Set 12"
       untypedTest.test = "15"; //nothing happens here
       Reflect.setProperty(untypedTest, "test", "18"); // "Set 18"
    }
}

I am a little confused by this.


SOLUTION github answer

Properties are resolved at compile time, which means the type must be known. See http://haxe.org/manual/class-field-property-type-system-impact.html

Reflect.setProperty starts at runtime ...

+4
source share

Source: https://habr.com/ru/post/1609658/


All Articles