Renderingla

Converts a value with explicit runtime parsing or coercion semantics.

Aliases: convert, conversio

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

Category

conversion

Related

Examples

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

Converts a value with explicit runtime parsing or coercion semantics.

# 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.

incipit {
    # ↦ converts textus → numerus
    sit n1 ← "42" ↦ numerus
    # ⇥ supplies recovery on failure
    sit n2 ← "invalid" ↦ numerus ⇥ 0
    sit n3 ← "255" ↦ numerus<i32>

    sit f1 ← "3.14159" ↦ fractus
    sit f2 ← "invalid" ↦ fractus ⇥ 0.0

    sit s1 ← 42 ↦ textus
    sit s2 ← 3.14 ↦ textus
    sit s3 ← verum ↦ textus

    # chained ↦ left-to-right
    sit roundtrip ← "42" ↦ numerus ↦ textus

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

Expected output:

42
0
255
3.14159
0.0
42
3.14
true
42