HaxeDoc2

(やまだぃちぅ) #1

1 var Main = function() {
2 var v = this.get_x();
3 this.set_x(2);
4 var _g = this;
5 _g.set_x(_g.get_x() + 1);
6 };
As specified, the read access generates a call toget_x(), while the write access generates a
call toset_x(2)where 2 is the value being assigned tox. The way the+=is being generated
might look a little odd at first, but can easily be justified by the following example:
1 class Main {
2 public var x(get, set):Int;
3 function get_x() return 1;
4 function set_x(x) return x;
5
6 public function new() { }
7
8 static public function main() {
9 new Main().x += 1;
10 }
11 }


What happens here is that the expression part of the field access toxin themainmethod
iscomplex: It has potential side-effects, such as the construction ofMainin this case. Thus, the
compiler cannot generate the+=operation asnew Main().x = new Main().x + 1and has
to cache the complex expression in a local variable:
1 Main.main = function() {
2 var _g = new Main();
3 _g.set_x(_g.get_x() + 1);
4 }

4.2.2 Impact on the type system.............................


The presence of properties has several consequences on the type system. Most importantly, it is
necessary to understand that properties are a compile-time feature and thusrequire the types to be
known. If we were to assign a class with properties toDynamic, field access wouldnotrespect
accessor methods. Likewise, access restrictions no longer apply and all access is virtually public.
When usinggetorsetaccess identifier, the compiler ensures that the getter and setter actu-
ally exists. The following problem does not compile:
1 class Main {
2 // Method get_x required by propertyx is
3 // missing
4 public var x(get, null):Int;
5 static public function main() {}
6 }
The methodget_xis missing, but it need not be declared on the class defining the property
itself as long as a parent class defines it:
1 class Base {
2 public function get_x() return1;
3 }
Free download pdf