→
Translation status: 简体中文 reader-locale proof. Term names and code fences follow the zh-Hans pack; supporting prose may still be English.
Success return type and recoverable alternate-exit type in function signatures.
Syntax: 函数 <name>(<params>) → <type> | 函数 <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 `抛错` and `捕获`.
#
# Common mistakes:
# • Forgetting the ⇥ error type in signatures that use 抛错.
#
# See also: ⇥, 函数, 返回, 抛错, 捕获
# =============================================================================
# operatores/function-types — → and ⇥
#
# GRAMMAR:
# funcType :← '函数' ident '(' params ')' ('→' type)? ('⇥' type)?
#
# EXPECTED OUTPUT:
# salve, 5, 0
#
# BACKEND:
# Cross-ref 函数/functio.fab and 抛错/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