It is also possible to omit the method body of a@:opfunction, but only if the underlying type
of the abstract allows the operation in question and if the resulting type can be assigned back to
please review for cor- the abstract.
rectness
please review for cor-
rectness
2.8.3 Array Access
Array access describes the particular syntax traditionally used to access the value in an array at
a certain offset. This is usually only allowed with arguments of typeInt. Nevertheless, with
abstracts it is possible to define custom array access methods. The Haxe Standard Library ( 10 )
You have marked uses this in itsMaptype, where the following two methods can be found:
“Map” for some rea-
son
You have marked
“Map” for some rea-
son
1 @:arrayAccess public inline function
2 get(key:K) return this.get(key);
3 @:arrayAccess public inline function
4 arrayWrite(k:K, v:V):V {
5 this.set(k, v);
6 return v;
7 }
There are two kinds of array access methods:
- If an@:arrayAccessmethod accepts one argument, it is a getter.
- If an@:arrayAccessmethod accepts two arguments, it is a setter.
The methodsgetandarrayWriteseen above then allow the following usage:
1 class Main {
2 public static function main() {
3 var map = new Map();
4 map["foo"] = 1;
5 trace(map["foo"]);
6 }
7 }
At this point it should not be surprising to see that calls to the array access fields are inserted
in the output:
1 map.set("foo",1);
2 1;
3 console.log(map.get("foo"));
Order of array access resolving Due to a bug in Haxe versions before 3.2 the order of checked
:arrayAccessfields was undefined. This was fixed for Haxe 3.2 so that the fields are now
consistently checked from top to bottom:
1 abstract AString(String){
2 public function new(s) this =s;
3 @:arrayAccess function getInt1(k:Int){
4 return this.charAt(k);
5 }
6 @:arrayAccess function getInt2(k:Int){
7 return this.charAt(k).toUpperCase();
8 }