渲染zh-Hans

Translation status: 简体中文 reader-locale proof. Code fences render through the zh-Hans pipeline; prose is canonical Latin.

Converts a value with explicit runtime parsing or coercion semantics.

Aliases: convert, conversio

Syntax: <expression> ↦ <type> [⇥ <recovery>]

Category#

conversion

Examples#

radix/corpus/conversio/conversio.fab (canonical · operator-group)#

Converts a value with explicit runtime parsing or coercion semantics.

# =============================================================================
# ↦ — Converts a value with explicit runtime parsing or coercion semantics.
# =============================================================================
#
# What this teaches:
#   • Core conversion — the `↦` operator converts between types with explicit parsing (textus→numerus) and coercion (numerus→textus).
#   • Recovery with `⇥` — the `⇥` operator supplies a fallback value when conversion fails at runtime.
#   • Chaining — `↦` composes left-to-right for roundtrip conversions.
#
# Common mistakes:
#   • Confusing `∷` (compile-time type ascription) with `↦` (runtime conversion) and using `vel` instead of `⇥` for conversio recovery.
#
# See also: ∷, ⇥
# =============================================================================

# conversio — type conversion operator (↦)
#
# <expr> ↦ <typus>
# <expr> ↦ <typus><params>
#
# GRAMMAR:
#   convertExpr :← expr '↦' type
#
# EXPECTED OUTPUT:
#   conversio.expected
#
# BACKEND:
#   Siblings radix.fab and bivalens.fab compile-only until Go semantics align.

main {
    # ↦ converts textus → numerus
    let n1  "42" ↦ int
    # ⇥ supplies recovery on failure
    let n2  "invalid" ↦ int ⇥ 0
    let n3  "255" ↦ int<i32>

    let f1  "3.14159" ↦ float
    let f2  "invalid" ↦ float ⇥ 0.0

    let s1  42 ↦ string
    let s2  3.14 ↦ string
    let s3  true ↦ string

    # chained ↦ left-to-right
    let roundtrip  "42" ↦ int ↦ string

    print n1
    print n2
    print n3
    print f1
    print f2
    print s1
    print s2
    print s3
    print roundtrip
}

Expected output:

42
0
255
3.14159
0.0
42
3.14
true
42