Skip to content

Generics

rockofox edited this page Jan 22, 2024 · 1 revision

Indigo supports generic types on functions.

trait Number
impl Number for Int
impl Number for Float

let add<N: Number> (a: N b: N) => N = do
  a + b
end

let main => IO = do
  println add 1, 2
end

In the above example, a function add is declared with a generic type parameter N constrained to the Number trait. This function takes two parameters of type N and returns a result of the same type.

The call to add only suceeds if the given parameters are of the same Number type. The return type N gets type erased to Number.

Clone this wiki locally