Skip to content

Generic verbs

Some operations that make sense for many types are built in, and resolved for each concrete type at compile time. For your own, write a generic function or declare a behavior.

Verb Works on Result
xs.len() any list Int
xs.contains(v) integers, floats, Str Bool
xs.reverse() any list the list reversed
xs.sum() List(Int), List(Float) the sum
xs.concat(ys) any list a new list
xs[a..b] any list a slice
sort(xs) numbers, Str, or a type with an Ord instance a sorted list
Int main() {
List(Int) xs = [3, 1, 2];
List(Str) ss = ["pear", "fig"];
List(Float) fs = [2.5, 0.5];
println(f"{xs.contains(2)} {ss.contains("fig")} {fs.contains(9.0)}");
println(f"{xs.sum()} {fs.sum()} {xs.reverse()} {sort(ss)}");
return 0;
}
Output
true true false
6 3 [2, 1, 3] ["fig", "pear"]

A function of your own with the same name, taking the list as its first parameter, takes precedence over the built-in verb.

abs(x), min(a, b), max(a, b) and clamp(x, lo, hi) take arguments of one numeric type, any width, and return that type:

Int main() {
Int(32) a = -9;
UInt(8) b = 250;
println(f"{abs(a)} {min(b, 3)} {max(1.25, 0.75)} {clamp(a, -5, 5)}");
return 0;
}
Output
9 3 1.25 -5

They are checked like any arithmetic: abs of the most negative integer traps.

sqrt(x) takes a Float (or a Float vector, lane by lane; see Vector types) and returns its correctly rounded IEEE 754 square root, one machine instruction; sqrt of a negative number is NaN.

Every width of Int, UInt, Float and Dec has built-in Eq, Ord and Hash, instantiated by the compiler for the concrete width. You never declare per-width instances.

Bool, Int(N) and UInt(N) also have a built-in Bounded(T), so x.min() and x.max() are the two’s complement bounds and a bare max() takes the type from its expected type (Int(8) top = max();). min(a, b) and max(a, b) with two arguments are the numeric builtins above; a T of your own orders two values with Ord’s a.least(b) and a.greatest(b) instead.