@return
@return ends a macro and, optionally, gives a value back to the caller.
Syntax
@return value
@return
Example
macro clamp(x: int, lo: int, hi: int) -> int {
@if x < lo {
@return lo
}
@if x > hi {
@return hi
}
@return x
}
macro show(value: int) {
@emit value
}
show clamp(-5, 0, 10)
show clamp(50, 0, 10)
show clamp(7, 0, 10)
0 10 7
Details
@returnstops the macro at once, even from inside@if,@match,@foror@fold.- A bare
@returnends the macro without a value. - Values the macro emitted before returning stay emitted.
@return f(...), wherefis the macro itself, is a tail call: it doesn’t count toward the recursion limit. See Recursion.@returncan return the value of a call that emits, and its emitted values are kept. See Returning values.
Returning early
macro first_multiple_of(k: int, limit: int) -> int {
@for i in 1..limit {
@if i % k == 0 {
@return i
}
}
@return -1
}
macro show(value: int) {
@emit value
}
show first_multiple_of(7, 100)
show first_multiple_of(700, 100)
7 -1