Bounds
When working with generics, the type parameters often must use traits as bounds to stipulate what functionality a type implements. For example, the following example uses the trait Display to print and so it requires T to be bound by Display; that is, T must implement Display.
// Define a function `printer` that takes a generic type `T` which
// must implement trait `Display`.
fn printer<T, +core::fmt::Display<T>, +Drop<T>>(t: T) {
println!("{}", t);
}
Bounding restricts the generic to types that conform to the bounds. That is:
#[derive(Drop)]
struct S<T, +core::fmt::Display<T>> {
value: T,
}
// Error! `Array<T>` does not implement `Display`. This
// specialization will fail.
fn foo() {
let _s = S { value: array![1] };
}
Another effect of bounding is that generic instances are allowed to access the methods of traits specified in the bounds. For example:
// A trait which implements the print marker: `{:?}`.
use core::fmt::Debug;
trait HasArea<T> {
fn area(self: @T) -> u64;
}
#[derive(Debug, Drop)]
struct Rectangle {
length: u64,
height: u64,
}
#[derive(Drop)]
struct Triangle {
length: u64,
height: u64,
}
impl RectangleArea of HasArea<Rectangle> {
fn area(self: @Rectangle) -> u64 {
*self.length * *self.height
}
}
// The generic `T` must implement `Debug`. Regardless
// of the type, this will work properly.
fn print_debug<T, +Debug<T>>(t: @T) {
println!("{:?}", t);
}
// `T` must implement `HasArea`. Any type which meets
// the bound can access `HasArea`'s function `area`.
fn area<T, +HasArea<T>>(t: @T) -> u64 {
HasArea::area(t)
}
fn main() {
let rectangle = Rectangle { length: 3, height: 4 };
let _triangle = Triangle { length: 3, height: 4 };
print_debug(@rectangle);
println!("Area: {}", area(@rectangle));
//print_debug(@_triangle);
//println!("Area: {}", area(@_triangle));
// ^ TODO: Try uncommenting these.
// | Error: Does not implement either `Debug` or `HasArea`.
}
As an additional note, trait bounds in Cairo are specified using the + syntax after the generic type parameter. Multiple bounds can be specified by adding additional + constraints.