Skip to main content

Type Guards

A type guard allows you to check a value at runtime and change the type at compile time to match. It's written with the is keyword, and evaluates to a Bool.

As a simple example, you can check the length of values:

fn main(bytes: Bytes) -> Bytes32 {
assert bytes is Bytes32;
bytes
}

After this assertion, the type of bytes is now Bytes32 since it has been checked.

A match expression makes the same checks as a type guard, and narrows the value in each of its arms the same way.

However, if you negate a type guard with the ! operator or use the value in an else branch, it will remain the Bytes type since there was no union that could be narrowed:

fn main(bytes: Bytes) -> Bytes {
assert !(bytes is Bytes32);
bytes
}

What's Checked​

The compiler emits the minimum set of checks needed to verify that the value on the left matches the type on the right. It skips any checks that are already implied by the type on the left. Depending on the types involved, it can check:

  1. Whether the value is an atom or a pair, such as value is Bytes or value is (Any, Any).
  2. The length of an atom, such as value is Bytes32 or value is PublicKey.
  3. The exact value of an atom, for literal types, such as value is 42 or value is nil.
  4. Each of the above for the first and rest of a pair, recursively.

Since a struct is a list at runtime, checking for a struct checks its structure and any fields with literal types. For example, every condition in the standard library is a variant of an enum that starts with its opcode, so if you already know that a value is a Condition, checking whether it's a CreateCoin only needs to compare the opcode to 51.

The value on the left is compiled on its own, without the type on the right being expected of it. That type is what the value is checked against, so it can't also be where the value gets its type from. For example, unchecked_cast(value) is Bytes32 is an error, since nothing says what type unchecked_cast should return. Write unchecked_cast::<Any>(value) is Bytes32 instead, which checks the value at runtime.

If the check is already decided at compile time, but evaluating the value could fail (such as a call to a function that raises), the value is still evaluated, so the failure isn't skipped.

Narrowing​

Type guards can be used to narrow the type of a union to one or more of its variants.

Let's say you have a union of structurally distinct types:

fn fee(condition: CreateCoin | ReserveFee) -> Int {
if condition is CreateCoin {
// The type is CreateCoin in this branch
0
} else {
// The type is ReserveFee in this branch
condition.amount
}
}

The else branch removes CreateCoin as a possibility, narrowing it down to the opposite, which is ReserveFee. This works regardless of how many variants you have in the union.

The narrowed type applies anywhere the result of the guard is known:

  1. In the branches of an if expression.
  2. In the arms of a match.
  3. After an if or match statement, using what's known in the branches that don't return or raise.
  4. After an assert statement.
  5. On the right side of && and ||, such as value is Int && value > 5.

Guards can be combined with !, &&, ||, &, and |, and the compiler will narrow each value based on what's known in each branch:

fn add(x: Int | (Int, Int), y: Int | (Int, Int)) -> Int {
if x is Int && y is Int {
x + y
} else {
0
}
}
warning

The type after is can be a union, so a | directly after a guard is read as part of the type. Wrap each guard in parentheses before combining them with |, such as (x is Int) | (y is Int). The same applies to casts with as.

Fields​

You can also guard a field of a pair or struct, such as pair.first or point.x. This narrows the type of that field wherever it's accessed through the same value:

struct Point {
x: Int | (Int, Int),
y: Int,
}

fn sum(point: Point) -> Int {
if point.x is Int {
point.x + point.y
} else {
point.y
}
}

This works for nested fields as well, such as outer.inner.x.

Pairs​

A guard can also check the parts of a pair at once, such as pair is (CreateCoin, Int). When the guard is false, the compiler works out exactly which combinations of the parts are left, so you can handle them one at a time:

fn fees(pair: (CreateCoin | ReserveFee, CreateCoin | ReserveFee)) -> Int {
if pair is (CreateCoin, CreateCoin) {
return 0;
}

if pair is (ReserveFee, ReserveFee) {
return pair.first.amount + pair.rest.amount;
}

if pair is (ReserveFee, CreateCoin) {
return pair.first.amount;
}

// The only combination left is `(CreateCoin, ReserveFee)`
pair.rest.amount
}

This is also how a match over every combination of a pair's parts is known to be exhaustive.

Semantics​

Narrowing only changes what's known about a value's structure, not its semantics. For example, if x is an Int, assert x is Bytes32; checks that it's 32 bytes long, but x is still an Int afterward, so x + x is an addition rather than a concatenation. The same goes for (x as Bytes) is Bytes32, which checks the cast value but narrows x itself. Likewise, a pair that's checked against a struct takes on the struct's fields, but not the struct itself.

To treat the value as the type you checked for, cast it after the guard, such as x as Bytes32. The cast is allowed, since the guard has already checked the structure.

A value of type Any (or Atom) has no semantics of its own, so it narrows to the type it's checked against.

Lints​

The compiler warns about type guards that don't do anything useful:

  • unnecessary_guard reports guards where the value is already known to be of the type, so a check isn't needed.
  • indistinguishable_guard reports guards that are always true at runtime, even though the types are different. For example, value is Int when value is Int | nil can never be false, since nil and 0 have the same runtime representation.
  • imprecise_narrowing reports guards (and match arms) on a struct with so many combinations of field types left over that the compiler merges them, which leaves the struct's fields wider than they could be afterward. Check fewer combinations at once to avoid it.

Limitations​

Some types can't be checked at runtime. Using them on the right side of a type guard is an error.

Function Types​

Rue supports higher-order functions, which means functions can be passed around freely as values. As such, it's possible to express functions as types.

However, there's no way to know at runtime whether or not a value fits the definition of a given function type. Any value could match the type, but not every value does match the type. On top of that, the same function is represented differently in debug and release builds, so a check that looked at a function value could behave differently between them.

Due to this, a type guard (or match arm) that would have to look at a function value is an error. This includes checking for nil in a union such as (fn() -> Int) | nil. A check that never has to look at the function is still allowed, such as value is nil when value is (fn() -> Int, nil) | nil, since telling a pair apart from nil doesn't depend on what's inside the pair.

Recursive Types​

A recursive type such as List<Int> can't be checked, since it would require a loop to check every item, and the length of the list isn't known at compile time.

However, you can check a recursive type against a non-recursive one, such as list is nil or list is (Int, List<Int>), since that only requires a fixed number of checks.

Generic Types​

A generic type parameter such as T could be any type, so there's no way to know what to check.

Large Types​

The compiler limits how deeply nested a type can be checked, and how large the check can be, to avoid generating an excessive amount of code. A very long list type such as [Int, Int, ..., Int] can't be checked all at once, and neither can a type whose check would be more than 10,000 nodes, such as one that nests a pair of itself many levels deep. Checks for typical types are under a hundred nodes.

Separately, comparing two very large or deeply recursive types can be too much work for the type checker, in which case it stops with an error such as "Comparing A to B is too complex", rather than taking too long to compile.

Overlapping Values​

Because different types can share the same runtime representation, a guard can only tell types apart if there's a value that belongs to one and not the other. In particular, nil is the same as 0, false, and "". So in a union like Int | nil, every value is also a valid Int, and value is Int is always true.

To represent an optional value that can be told apart from nil, use a type that never has that value, such as Bytes32, a pair, or a struct.