渲染zh-Hant

conversio-fallibilis

Translation status: 繁體中文 reader-locale proof. Term names and code fences follow the zh-Hant pack; supporting prose may still be English.

Three recovery postures for fallible 轉換: inline ⊥ default, ⇥ propagation, and 執行/cape.

Syntax: <expression> ↦ <type> | 函式 … → T ⇥ E | 執行 { … } 捕捉 err { … }

Category#

conversion

Examples#

radix/corpus/conversio/fallibilis.fab (canonical · conversio)#

Three recovery postures for fallible 轉換: inline ⊥ default, ⇥ propagation, and 執行/cape.

# =============================================================================
# conversio-fallibilis — Three recovery postures for fallible 轉換: inline ⊥ default, ⇥ propagation, and 執行/cape.
# =============================================================================
#
# What this teaches:
#   • Recovery postures — three strategies for handling 變換 failure: inline `⊥` default, `⇥` propagation to callers, and `執行 { … } 捕捉 err { … }` error absorption.
#   • Type signatures — how `→ T ⇥ 文字` propagates 變換 error as a typed channel.
#
# Common mistakes:
#   • Mixing propagation and inline recovery — bare `↦` without `⊥` inside a `→ T ⇥ 文字` function still propagates errors rather than handling them locally.
#
# See also: ↦, ⇥, ⊥, 執行, 捕捉, 拋出
# =============================================================================

# 轉換 fallibilis — inline ⊥ default, ⇥ propagation, and 執行/cape recovery
#
# Posture 1: `expr ↦ T ⊥ recovery` handles failure at the site.
# Posture 2: bare `↦` inside `→ T ⇥ 文字` propagates to callers.
# Posture 3: `執行 { … }` with `捕捉 err { … }` absorbs propagated 轉換 failure.
#
# BACKEND:
#   Rust lowers all three postures (inline `⊥`, propagation, `執行`/`捕捉`).
#   Go/TS remain diagnostic-only until their failable stacks mature.

# Why: this function is itself the `⊥` default below, so it cannot default
# itself; epoch seconds 0 cannot fail to convert, so it needs no handler.
fn epochZero() → instant {
    return 0 ↦ instant
}

fn parseInstans(value v) → instant ⇥ string {
    return v ↦ instant
}

fn inlineRecovery(value v) → instant {
    return v ↦ instant ⊥ epochZero()
}

fn tutum(value v) → instant {
    do {
        return parseInstans(v)
    }
    catch err {
        warn err
        return epochZero()
    }
}

fn tutumDirect(value v) → instant {
    do {
        return v ↦ instant
    }
    catch err {
        warn err
        return epochZero()
    }
}

main {
    const value good ← "1979-05-27T07:32:00Z"
    const value bad ← "not-a-datetime"
    assert inlineRecovery(good) ≡ (good ↦ instant)
    assert inlineRecovery(bad) ≡ epochZero()
    assert tutum(good) ≡ (good ↦ instant)
    assert tutum(bad) ≡ epochZero()
    assert tutumDirect(good) ≡ (good ↦ instant)
    assert tutumDirect(bad) ≡ epochZero()
    print inlineRecovery(good), tutum(good), tutumDirect(good)
}