Avoid setter call inside class code

74 views
Skip to first unread message

T1m_

unread,
Apr 1, 2015, 2:02:50 AM4/1/15
to haxe...@googlegroups.com
From Haxe manual (http://haxe.org/manual/class-field-property-rules.html) "Both getter and setter may access their physical field for data storage. The compiler ensures that this kind of field access does not go through the accessor method if made from within the accessor method itself, thus avoiding infinite recursion:"

Is there a way to get separate access for setter and for physical field in any other place of code?

public var prop(default, set):Float;

public function new () {
    prop
= 10;    // actually this is a set_prop(10);

   
// will be great to do this;
    prop
= 10;    // not the set_prop(10) !
   
   
// but if we want call accessor
    set_prop
(10);
}

private function set_prop(v:Float):Float {
   
// here can be a lot of code of initialization, checks or/and access to other objects which can be null now
   
return prop = v;
}

Current solution for this - to use "get" instead "default" and declare "private var _prop:Float".

Gama11

unread,
Apr 2, 2015, 4:27:43 AM4/2/15
to haxe...@googlegroups.com
In theory, you could use Reflect.setField() to bypass the setter, but that would be a horrible hack. ;)

I don't think there's a better solution than the "get instead of default" one you suggested yourself (not even a bad solution IMO).
Reply all
Reply to author
Forward
0 new messages