Iterators – Haxe – The Cross-platform Toolkit

  1. Home
  2. map design tutorials
  3. Iterators – Haxe – The Cross-platform Toolkit

With Haxe it is very easy to define custom iterators and iterable data types. These concepts are represented by the types Iterator&lt,T&gt, and Iterable&lt,T&gt, respectively: Any class which structurally unifies with one of these types can be iterated over using a for-loop. That is, if the class defines methods hasNext and next with matching return types it is considered an iterator, if it defines a method iterator returning an Iterator&lt,T&gt, it is considered an iterable type. The type MyStringIterator in this example qualifies as iterator: It defines a method hasNext returning Bool and a method next returning String, making it compatible with Iterator&lt,String&gt,. The main method instantiates it, then iterates over it. Here we do not setup a full iterator like in the previous example, but instead define that the MyArrayWrap&lt,T&gt, has a method iterator, effectively forwarding the iterator method of the wrapped Array&lt,T&gt, type. Source.

whatsapp button