HaxeDoc2

(やまだぃちぅ) #1
12 }

13
14 public function next() {
15 return s.charAt(i++);
16 }
17 }
18
19 class Main {
20 static public function main() {
21 var myIt = new MyStringIterator("string");
22 for (chr in myIt){
23 trace(chr);
24 }
25 }
26 }


The typeMyStringIteratorin this example qualifies as iterator: It defines a method
hasNextreturningBooland a methodnextreturningString, making it compatible with
Iterator. Themainmethod instantiates it, then iterates over it.
1 class MyArrayWrap{
2 var a:Array;
3 public function new(a:Array) {
4 this.a = a;
5 }
6
7 public function iterator() {
8 return a.iterator();
9 }
10 }
11
12 class Main {
13 static public function main() {
14 var myWrap = new MyArrayWrap([1, 2, 3]);
15 for (elt in myWrap){
16 trace(elt);
17 }
18 }
19 }


Here we do not setup a full iterator like in the previous example, but instead define that the
MyArrayWrap<T>has a methoditerator, effectively forwarding the iterator method of the
wrappedArray<T>type.

6.8 Function Bindings......................................


Haxe 3 allows binding functions with partially applied arguments. Each function type can be
considered to have abindfield, which can be called with the desired number of arguments in
order to create a new function. This is demonstrated here:
1 class Bind {
2 static public function main() {
3 var map = new Map<Int,String>();
Free download pdf