Kết xuấtvi

conversio-fallibilis

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

Three recovery postures for fallible chuyển_đổi: inline ⊥ default, ⇥ propagation, and làm/cape.

Syntax: <expression> ↦ <type> | hàm … → T ⇥ E | làm { … } bắt err { … }

Category#

conversion

Examples#

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

Three recovery postures for fallible chuyển_đổi: inline ⊥ default, ⇥ propagation, and làm/cape.

# =============================================================================
# conversio-fallibilis — Three recovery postures for fallible chuyển_đổi: inline ⊥ default, ⇥ propagation, and làm/cape.
# =============================================================================
#
# What this teaches:
#   • Recovery postures — three strategies for handling chuyển failure: inline `⊥` default, `⇥` propagation to callers, and `làm { … } bắt err { … }` error absorption.
#   • Type signatures — how `→ T ⇥ văn_bản` propagates chuyển error as a typed channel.
#
# Common mistakes:
#   • Mixing propagation and inline recovery — bare `↦` without `⊥` inside a `→ T ⇥ văn_bản` function still propagates errors rather than handling them locally.
#
# See also: ↦, ⇥, ⊥, làm, bắt, ném
# =============================================================================

# chuyển_đổi fallibilis — inline ⊥ default, ⇥ propagation, and làm/cape recovery
#
# Posture 1: `expr ↦ T ⊥ recovery` handles failure at the site.
# Posture 2: bare `↦` inside `→ T ⇥ văn_bản` propagates to callers.
# Posture 3: `làm { … }` with `bắt err { … }` absorbs propagated chuyển_đổi failure.
#
# BACKEND:
#   Rust lowers all three postures (inline `⊥`, propagation, `làm`/`bắt`).
#   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)
}