Renderingen-US

conversio-fallibilis

Translation status: English reader-locale proof. Term names and code fences follow the en pack; supporting prose may still be English.

Three recovery postures for fallible conversion: inline ⊥ default, ⇥ propagation, and do/cape.

Syntax: <expression> ↦ <type> | fn … → T ⇥ E | do { … } catch err { … }

Category#

conversion

Examples#

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

Three recovery postures for fallible conversion: inline ⊥ default, ⇥ propagation, and do/cape.

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

# conversion fallibilis — inline ⊥ default, ⇥ propagation, and do/cape recovery
#
# Posture 1: `expr ↦ T ⊥ recovery` handles failure at the site.
# Posture 2: bare `↦` inside `→ T ⇥ string` propagates to callers.
# Posture 3: `do { … }` with `catch err { … }` absorbs propagated conversion failure.
#
# BACKEND:
#   Rust lowers all three postures (inline `⊥`, propagation, `do`/`catch`).
#   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)
}