@assert
@assert stops compilation with an error if a condition is false.
Syntax
@assert condition
@assert condition, "message"
The message is optional, and must be a string literal.
Example
macro shift_amount(n: int) {
@assert n >= 0 && n < 32, "shift amount must be 0 to 31"
@emit n
}
shift_amount 5
5
macro shift_amount(n: int) {
@assert n >= 0 && n < 32, "shift amount must be 0 to 31"
@emit n
}
shift_amount 40
shift amount must be 0 to 31
Details
- The condition is true if it’s not
0. - A passing
@assertemits nothing and has no other effect. - The error points at the
@assertthat failed.
Asserts, hooks and invariants
An @assert checks something at one point in one macro. For checks that
apply more broadly:
- To check the arguments of several macros the same way, put the asserts in
one macro and call it from a
beforehook. - To check every value of a type, wherever it’s made, use an invariant.