ProjectMoon/eve2

Cloning dynamic field to variable does not preserve getters/setters

Closed this issue · 1 comments

Cloning (implicit or otherwise) does not clone the getters and setters when cloning to a single variable. What happens is it pulls the value from the dynamic field and sets the new variable to it. Getters and setters are lost in the transfer. This only seems to happen if a dynamic field is assigned directly to a new variable:

var privateY = 0;
var x = {
        y: -1
};

x.y->get = () {
        println("getting " ~ global::privateY);
        return global::privateY;
};

x.y->set = (value) {
        println("setting " ~ value);
        global::privateY = value;
};

//here, x.y and z.y will become marked for clone and everything seems to be preserved.
var z = clone x;
z.y = 10;
println("printing z");
println(z.y);

//here, the getters and setters are not preserved.
var z = clone x.y;
z = 10;
println(z);


As it turns out, this is not an issue because of how dynamic fields work! The interpreter properly returns the value from the getter and then that is cloned. In the example above we should have cloned x->y to get the dynamic field by itself into the variable z.