Skip to content

Casts and Type Checks

Calibre provides several cast forms for converting between values and types.

The basic cast form is as.

let result := "64" as int;

as returns a result value so that failed conversions can be handled explicitly.

match "64" as int {
.Ok : value => print(value),
.Err : msg => print(msg)
};

If you want an option instead of a result, use as?.

let maybe_number := "64" as? int;
let failed := "oops" as? int;

If the conversion should be forced, use as!.

This panics if the conversion fails, so it is best used when failure would be exceptional or impossible.

let number := "64" as! int;

try works naturally with casts.

This is often the most convenient way to say “convert this value, but fall back if the cast fails.”

let number := try "64" as int => 0;

Calibre also supports type checks with is.

You will most often see is in pattern matching, especially with dynamic dispatch and more general match arms.

match value {
is Locked => print("locked"),
is Unlocked => print("unlocked"),
_ => print("other")
};