Kết xuấtvi

→

Translation status: Tiếng Việt reader-locale proof. Term names and code fences follow the vi pack; supporting prose may still be English.

Success return type and recoverable alternate-exit type in function signatures.

Syntax: hàm <name>(<params>) → <type> | hàm <name>(<params>) → <success> ⇥ <error>

Category#

function

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 `ném` and `bắt`.
#
# Common mistakes:
#   • Forgetting the ⇥ error type in signatures that use ném.
#
# See also: ⇥, hàm, trả, ném, bắt
# =============================================================================

# operatores/function-types — → and ⇥
#
# GRAMMAR:
#   funcType :← 'hàm' ident '(' params ')' ('→' type)? ('⇥' type)?
#
# EXPECTED OUTPUT:
#   salve, 5, 0
#
# BACKEND:
#   Cross-ref hàm/functio.fab and ném/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