January 2019
Intermediate to advanced
392 pages
10h 11m
English
A for loop statement allows us to iterate anything that contains the iterate() method. In turn, this provides an instance that matches the iterator interface through the principle of duck typing.
The Iterator interface looks as follows:
public interface Iterator<E> { boolean hasNext(); E next();}
If we want to provide the iterator(), hasNext(), and next() methods as class members, we have to declare them with the operator keyword. The following example demonstrates a case of this:
class Numbers(val numbers: Array<Int>) { private var currentIndex: Int = 0 operator fun iterator(): Numbers = Numbers( ...
Read now
Unlock full access