Generic Interfaces

Generics can be used to define interfaces. Take Iterable defined in the standard library as an example. Its member function iterator needs to return an Iterator type, which is a container traverser. Iterator is a generic interface. It has a member function next, which returns the next element from the container type. When using this function, you need to specify the type of element you anticipate it to return. Therefore, Iterator needs to declare the generic parameter.

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
}