→
Translation status: English reader-locale proof. Term names and code fences follow the en pack; supporting prose may still be English.
Success return type and recoverable alternate-exit type in function signatures.
Syntax: fn <name>(<params>) → <type> | fn <name>(<params>) → <success> ⇥ <error>
Category#
function
Related#
Examples#
radix/corpus/operatores/function-types.fab (canonical · operator-group)#
Success return type and recoverable alternate-exit type in function signatures.
# =============================================================================
# → — Success return type and recoverable alternate-exit type in function signatures.
# =============================================================================
#
# What this teaches:
# • Return type annotation — `→` separates a function signature from its success return type.
# • Fallible return — `⇥` after `→` declares a recoverable error type, paired with `throw` and `catch`.
#
# Common mistakes:
# • Forgetting the ⇥ error type in signatures that use throw.
#
# See also: ⇥, fn, return, throw, catch
# =============================================================================
# operatores/function-types — → and ⇥
#
# GRAMMAR:
# funcType :← 'fn' ident '(' params ')' ('→' type)? ('⇥' type)?
#
# EXPECTED OUTPUT:
# salve, 5, 0
#
# BACKEND:
# Cross-ref fn/functio.fab and throw/functio-fallibilis.fab.
fn saluta() → string {
return "salve"
}
fn divide(int a, int b) → int ⇥ string {
if b ≡ 0 then throw "division by zero"
return a / b
}
fn tutum(int a, int b) → int {
do {
return divide(a, b)
}
catch err {
warn err
return 0
}
}
main {
print saluta()
print tutum(10, 2)
print tutum(10, 0)
}Expected output:
salve
5
0