Generic Interfaces
Interfaces can be declared as generic. Take the Iterable<E>
interface defined in the standard library as an example. Its member function iterator
needs to return a value of the type Iterator<E>
, a traversal object for a container or sequence. Iterator<E>
is also a generic interface, which includes the next
member function that returns the next element from the underlying. The return value type of the next
member function needs to be specified only at points of use. The type parameter E
of Iterator<E>
serves that need:
public interface Iterable<E> {
func iterator(): Iterator<E>
}
public interface Iterator<E> <: Iterable<E> {
func next(): Option<E>
}
public interface Collection<T> <: Iterable<T> {
prop size: Int64
func isEmpty(): Bool
}