HaxeDoc2

(やまだぃちぅ) #1
Trivia: Accessor names
In Haxe 2, arbitrary identifiers were allowed as access identifiers and would lead to custom
accessor method names to be admitted. This made parts of the implementation quite tricky
to deal with. In particular,Reflect.getProperty()andReflect.setProperty()had
to assume that any name could have been used, requiring the target generators to generate
meta-information and perform lookups.
We disallowed these identifiers and went for theget_andset_naming convention which
greatly simplified implementation. This was one of the breaking changes between Haxe 2
and 3.

4.2.1 Common accessor identifier combinations


The next example shows common access identifier combinations for properties:
1 class Main {
2 // read from outside, write onlywithin Main
3 public var ro(default, null):Int;
4
5 // write from outside, read onlywithin Main
6 public var wo(null, default):Int;
7
8 // access through getter get_xand setter
9 // set_x
10 public var x(get, set):Int;
11
12 // read access through getter,no write
13 // access
14 public var y(get, never):Int;
15
16 // required by field x
17 function get_x() return 1;
18
19 // required by field x
20 function set_x(x) return x;
21
22 // required by field y
23 function get_y() return 1;
24
25 function new() {
26 var v = x;
27 x = 2;
28 x += 1;
29 }
30
31 static public function main() {
32 new Main();
33 }
34 }


The Javascript output helps understand what the field access in themain-method is compiled
to:
Free download pdf