Generic functions¶
Some functions work the same way for every element type. The last element of a list is found the same way whether the list holds titles or page counts. A generic function is written once with a type parameter, a capital letter in angle brackets after the name, and each call fills it in:
fn last_of<T>(xs: List<T>) : Option<T> = xs[-1]
[last_of(["Dune", "Emma"]), last_of([] | List<String>)]
["Emma", none]
Called with a List<String>, T is String and the result is an
Option<String>. Called with a List<Int>, the result is an Option<Int>:
fn last_of<T>(xs: List<T>) : Option<T> = xs[-1]
last_of([412, 280])
Option<Int>
Bounds¶
Inside the body, T can be any type, so the body can only do what works
for every type. Finding the greatest element needs an order, and not every
type has one. The checker rejects the body:
fn longest<T>(xs: List<T>) : Option<T> = xs | max
longest([412, 280])
check error[unsatisfied-bound]: `max` needs elements with an order of their own, and T has none; sort with an explicit key first
--> <expression>:1:47
|
1 | fn longest<T>(xs: List<T>) : Option<T> = xs | max
| ^^^
A bound restricts the type parameter. T : Orderable says T has an
order, so the body may use max, min and sort:
fn longest<T : Orderable>(xs: List<T>) : Option<T> = xs | max
longest([412, 280, 310])
412
The bound is now part of the contract, and each call is checked against it. A set has no order of its own, so a list of sets is rejected:
fn longest<T : Orderable>(xs: List<T>) : Option<T> = xs | max
longest([{1}, {2}])
check error[type-mismatch]: `longest` needs T : Orderable, and T is Set<Int> here
--> <expression>:2:1
|
2 | longest([{1}, {2}])
| ^^^^^^^^^^^^^^^^^^^
A bound is a type or a trait. Traits shows how to declare traits and use them as bounds.