Examples

Generics and interfaces

Type parameters, and the methods a type must have.

main (print)

# A type parameter in brackets. Any places no requirement on it.
first[a Any](xs &[a], fallback a) a
    if len(xs) == 0
        fallback
    xs[0]

# An interface lists methods; a type satisfies it by declaring them.
{Legged:
    (&self) legs() int}

<cat: tag str>
(c &cat) legs() int
    4

count[a Legged](thing &a) none
    print.line("{thing.legs()} legs")

main()
    xs = [int: 7, 8]
    tom = <cat: tag = "tom">
    shown = count(tom)
    print.line("{first(xs, 0)}")
4 legs
7