Pelago
Core typesInterfaces

Interfaces

The interfaces declared in core: show, eq, order, hash, clone, the operator interfaces and converts, and which types have them already.

An interface lists methods a type can provide. core declares the ones the language itself relies on: printing, comparing, hashing, copying and the operators. A type gets one in three ways: it is built in (the primitives and collections), it is derived with @derive(...), or you write impl name for type Ellipsis.

InterfaceMethodEnablesPage
showfn show(self) -> stringprint, #( ) stringsshow
eqfn eq(self, other: Self) -> bool==, !=eq
order : eqfn cmp(self, other: Self) -> int< > <= >=, sorting, min, maxorder
hash : eqfn hash(self) -> intmap keys, set membershash
clonefn clone(self) -> Selfcopy on an objclone
add sub mul div rem negfn add(self, other: Self) -> Self and so on+ - * / % and unary -operators
converts<E>fn convert(self) -> E? between error typesconverts

What each kind of type has

Typeshoweqorderhashclone
bool, char, integers, stringyesyesno for bool, yes for the restyesyes
floatyesyesyesno meaningfulyes
list, map, set, tupleswhen elements arewhen elements arelists when elements arenoyes
struct, enum you writewrite @derive(show) or impl showautomatic@derive(order) or impl orderimpl hashautomatic
obj you writesame as struct@derive(eq) or impl eq@derive(order) or impl order@derive(hash) or impl hash@derive(clone) or impl clone

Using an interface as a bound

A generic function can require an interface with a bound, and a list can hold values of different types through any:

fn biggest<T: order>(a: T, b: T) -> T = a > b ? a : b

fn show_all(xs: [any show]) {
    for x of xs {
        print(x)
    }
}

fn main() {
    print(biggest(3, 9), biggest("a", "b"), biggest(2.5, 1.5))
    show_all([1, "a", 2.5, true])
}
9 b 2.5
1
a
2.5
true

Types guide, Generics

On this page