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<,T>, and Iterable<,T>, 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<,T>, 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<,String>,. 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<,T>, has a method iterator, effectively forwarding the iterator method of the wrapped Array<,T>, type. Source.

Яндекс.Метрика Рейтинг Free Web Counter
page counter
Last Modified: April 18, 2016 @ 2:08 am