Skip to content

TypeParameters

Christophe Pollet edited this page Jul 24, 2019 · 2 revisions

Type parameters

Type parameters correspond to the Java generics.

Types and classes

A type can be parameterized like so:

type List[T] {
 ...
}

When creating a new implementation of this list that can only contain elements of type Number (lets say that it's optimized for that), we use:

class IntegerList : List[Number] {
  ...
}

Of course one can subtype with type parameters:

type Whatever[T] : Supertype[String] {
}

This defines a Whatever type that can be parameterized, itself subtyping the Supertype[String] type.

Methods

It is also possible to have type parameters on methods. The are expressed like so:

apply[T, U](val: T): U;

Clone this wiki locally