Skip to content

Dynamic Dispatch

Calibre supports dynamic dispatch with dyn:<Trait>.

This lets a value be treated as “something that implements this trait” without requiring one specific concrete type.

const show := fn (label : str, p : dyn:<Human>) => {
print(label & " -> name=" & p.name & ", age=" & p.age & ", country=" & p.country);
};

Here, show can accept any value that implements Human.

This is often useful when different concrete types should be handled through one shared interface.

let ty := Ty { };
let tadiwa := Tadiwa { };
show("ty", ty);
show("tadiwa", tadiwa);

They also work well together with matching and type checks.

match state {
is Locked => print("Locked"),
is Unlocked => print("Unlocked"),
is Alarm => print("Alarm")
};