Renderingen-US

Experimental through version 1. Version 0 was alpha. This is the first language release: the interface is relatively stable, not frozen. Nothing is promised stable until version 2.

from

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

Extracts fields from a value into local bindings.

Syntax: from <expression> <let|mut> <field> [, ...] [rest <ident>]

Category#

binding

Examples#

radix/corpus/reference/keyword/ex/destructuring.fab (canonical · keyword)#

Extracts fields from a value into local bindings.

# =============================================================================
# from — Extracts fields from a value into local bindings.
# =============================================================================
#
# What this teaches:
#   • Loop-binder destructuring — `for from xs let [a, b] { ... }` binds each
#     element through the same pattern grammar as binding-position forms.
#   • Records (class) — `for from puncta let {x, y} { ... }` object_pattern
#     binds fields by name on each element.
#   • Positional tuples — `for from ordines let [a, b] { ... }` binds tuple
#     slots positionally.
#   • Rest patterns — `for from matricula let [caput, rest cauda]` captures
#     the remaining list.
#   • Holes — `for from ordines let [_, secundus]` skips a slot with `_`.
#
# Common mistakes:
#   • Binding more pattern slots than the tuple element has — the loop binder
#     inherits the binding-form width rules and rejects with
#     `tuple_index_out_of_bounds`.
#   • Naming an element label the tuple does not declare — by-label patterns
#     must name declared labels and reject with `unknown_tuple_element_label`.
#
# See also: ref, as, rest
# =============================================================================

# for — destructuring loop binders
#
# for from <collection> <let|mut> <pattern> { <body> }
#
# The binder accepts the same array/object patterns as binding position:
#   [a, b]              positional slots
#   {x, y}              record fields
#   [caput, rest cauda] rest pattern
#   [_, x]              holes
#
# GRAMMAR:
#   forInStmt :← 'for' 'from' expr ('let' | 'mut') pattern block
#
# EXPECTED OUTPUT:
#   destructuring.expected — sums and elements from every pattern form.

class Punctum {
    mut int x
    mut int y
}

main {
    # --- Records: object_pattern over list of class elements ---

    let _ puncta ← [Punctum { x = 1, y = 2 }, Punctum { x = 3, y = 4 }]
    for from puncta let {x, y} {
        print x + y
    }

    # --- Labeled tuples: object_pattern binds by label ---

    let list<tuple<gx: int, int>> paria ← [tuple<int, int> [50, 60], tuple<int, int> [70, 80]]
    for from paria let {gx} {
        print gx
    }

    # --- Positional tuples: array_pattern over fixed-width rows ---

    let list<tuple<int, int>> ordines ← [tuple<int, int> [10, 20], tuple<int, int> [30, 40]]
    for from ordines let [a, b] {
        print a + b
    }

    # --- Rest patterns: first element plus the remaining list ---

    let _ matricula ← [[1, 2, 3], [4, 5, 6]]
    for from matricula let [caput, rest cauda] {
        print caput
        print cauda[1]
    }

    # --- Holes: `_` skips a slot ---

    for from ordines let [_, secundus] {
        print secundus
    }
}

Expected output:

3
7
50
70
30
70
1
3
4
6
20
40

radix/corpus/reference/keyword/ex/ex-in-itera.fab (canonical · keyword)#

Extracts fields from a value into local bindings.

# =============================================================================
# from — Extracts fields from a value into local bindings.
# =============================================================================
#
# What this teaches:
#   • value iteration (`for from`) over list elements — the primary workhorse for iterating over collections
#   • transforming values inside the loop body (e.g., doubling each element)
#
# Common mistakes:
#   • Using from (value iteration) when ref (key/index iteration) is needed — from iterates values, ref iterates keys or indices.
#
# See also: ref, as, rest
# =============================================================================

# for from — for-in value iteration over listas
#
# for from <collection> let <item> { <body> }
#
# GRAMMAR:
#   forInStmt :← 'for' 'from' expr 'let' ident block
#
# EXPECTED OUTPUT:
#   ex.expected — numbers, names, and doubled values.

main {
    # Iterate over number list
    let list<int, size 5> numeri ← [1, 2, 3, 4, 5]
    for from numeri let n {
        print n
    }

    # Iterate over string list
    let list<string, size 3> nomina ← ["Marcus", "Julia", "Claudia"]
    for from nomina let name {
        print name
    }

    # Transform values
    let list<int, size 3> valores ← [10, 20, 30]
    for from valores let v {
        let int duplex ← v * 2
        print duplex
    }
}

Expected output:

1
2
3
4
5
Marcus
Julia
Claudia
20
40
60

radix/corpus/reference/keyword/ex/ex.fab (canonical · keyword)#

Extracts fields from a value into local bindings.

# =============================================================================
# from — Extracts fields from a value into local bindings.
# =============================================================================
#
# What this teaches:
#   • Field extraction — `from <expr> let <field>, ...` extracts named fields from class records into local scope.
#   • Multiple field extraction — extract several fields from a single expression in one statement.
#
# Common mistakes:
#   • Mode mismatch — passing a `ref` (read-only) parameter to an `from` (consume) position, or extracting a field that doesn't exist on the target type.
#
# See also: ref, as, rest
# =============================================================================

# from — field extract statement
#
# GRAMMAR:
#   extractStmt :← 'from' expr bindingList
#
# EXPECTED OUTPUT:
#   Scalar stdout smoke (see body).
#
# BACKEND:
#   Cross-ref: destructura/objectum.fab, for/ex.fab (for from mode).
#

class Persona {
    mut string nomen
    mut int aetas
}

main {
    let _ p ← Persona { nomen = "Marcus", aetas = 30 }
    let string nomen ← p.nomen
    let int aetas ← p.aetas
    print nomen
    print aetas
}

Expected output:

Marcus
30

radix/corpus/reference/keyword/ex/lista.fab (canonical · keyword)#

Extracts fields from a value into local bindings.

# =============================================================================
# from — Extracts fields from a value into local bindings.
# =============================================================================
#
# What this teaches:
#   • Lista destructuring — `let [a, b, c] ← list` extracts elements from a list into individual bindings.
#   • Rest patterns — `let [caput, rest cauda] ← list` captures remaining elements.
#   • Mutable destructuring — `mut [x, y] ← list` for mutable bindings.
#   • Skip and nested patterns — underscore `_` skips elements; nested listas destructure recursively.
#
# Common mistakes:
#   • Destructuring fewer or more elements than the list contains without handling bounds — pattern must match or the destructuring will fail.
#
# See also: ref, as, rest
# =============================================================================

# destructura — list destructuring patterns
#
# let [a, b, c] ← list
# mut [x, y, z] ← list
# let [caput, rest cauda] ← list
# let [_, medius, _] ← list
#
# GRAMMAR:
#   destructStmt :← ('let' | 'mut') '[' patternList ']' '←' expr
#
# EXPECTED OUTPUT:
#   Element values from inline and bound listas, rest slices, nested rows.

main {
    # --- Basic list destructuring ---

    let _ numeri ← [1, 2, 3]
    let [a, b, c] ← numeri
    print a
    print b
    print c

    # Destructure inline list literal
    let [primus, secundus, tertius] ← [10, 20, 30]
    print primus
    print secundus
    print tertius

    # --- Mutable destructuring with mut ---

    let _ coordinata ← [100, 200]
    mut [x, y] ← coordinata
    print x
    print y
    x ← x + 50
    y ← y + 50
    print x
    print y

    # --- Partial destructuring (fewer variables than elements) ---

    let _ valores ← [1, 2, 3, 4, 5]
    let [unus, duo] ← valores
    print unus
    print duo

    # --- Rest pattern with rest ---

    let _ res ← [1, 2, 3, 4, 5]
    let [caput, rest cauda] ← res
    print caput
    print cauda

    # --- Skip elements with underscore ---

    let _ triplex ← [10, 20, 30]
    let [_, medius, _] ← triplex
    print medius

    # --- Nested listas ---

    let _ matrix ← [[1, 2], [3, 4]]
    let [ordo1, ordo2] ← matrix
    print ordo1
    print ordo2
}

Expected output:

1
2
3
10
20
30
100
200
150
250
1
2
1
[2, 3, 4, 5]
20
[1, 2]
[3, 4]

radix/corpus/reference/keyword/ex/objectum.fab (canonical · keyword)#

Extracts fields from a value into local bindings.

# =============================================================================
# from — Extracts fields from a value into local bindings.
# =============================================================================
#
# What this teaches:
#   • Field destructuring — `from <expr> let <field>, ...` extracts named fields from class records into local bindings.
#   • Aliasing — `from <expr> let <field> as <alias>` renames extracted fields.
#   • Mutable extraction — `from <expr> mut <field>, ...` for mutable field bindings.
#   • Rest pattern — `from <expr> let <field>, rest <rest>` binds `<rest>` to the whole source value, which still holds every field.
#   • Nested paths — `from <expr>.<field> let ...` destructures through nested paths.
#
# Common mistakes:
#   • Destructuring a field that does not exist on the target type — the field must be present in the class definition or the type must support open shapes.
#
# See also: ref, as, rest
# =============================================================================

# destructura — field destructuring with `from`
#
# from <expr> let <field>, ...
# from <expr> mut <field>, ...
# from <expr> let <field> as <alias>
# from <expr> let <field>, rest <rest>
#
# Field destructuring works on class records (fixed fields) and map/valor
# (open shapes). This tour uses class types so that field names are statically
# known and codegen-complete.
#
# GRAMMAR:
#   destructStmt :← 'from' expr ('let' | 'mut') patternList
#
# EXPECTED OUTPUT:
#   objectum.expected — extracted field values, aliased names, mutated mut
#   bindings, and the rest binding printing the whole source record.

class Persona {
    mut string nomen
    mut int aetas
    mut string urbs
}

class Civis {
    mut string nomen
    mut string epistula
}

class Datum {
    mut int numerus
    mut bool activus
}

class Forma {
    mut string hospes
    mut int portus
    mut bool tutus
}

class Plenus {
    mut int signum
    mut string nomen
    mut string epistula
    mut string munus
}

class Sodalis {
    mut string nomen
    mut bool probatus
}

class Relatus {
    mut Sodalis civis
}

class Responsum {
    mut Relatus datum
}

class Vestis {
    mut string modus
    mut string lingua
}

main {
    # --- Basic field extraction ---

    let _ persona ← Persona { nomen = "Marcus", aetas = 30, urbs = "Roma" }
    let string nomen ← persona.nomen
    let int aetas ← persona.aetas
    print nomen
    print aetas

    # --- Extract with alias using 'as' ---

    let _ civis ← Civis { nomen = "Julia", epistula = "julia@roma.com" }
    let string cognomen ← civis.nomen
    let string epistula2 ← civis.epistula
    print cognomen
    print epistula2

    # --- Mutable destructuring with mut ---

    let _ datum ← Datum { numerus = 100, activus = true }
    mut int numerus ← datum.numerus
    mut bool activus ← datum.activus
    print numerus
    print activus
    numerus ← 200
    activus ← false
    print numerus
    print activus

    # --- Mixed alias and regular fields ---

    let _ forma ← Forma { hospes = "localis", portus = 8080, tutus = true }
    let string hospes ← forma.hospes
    let int porta ← forma.portus
    let bool tutus ← forma.tutus
    print hospes
    print porta
    print tutus

    # --- Rest pattern with rest ---

    let _ plenus ← Plenus { signum = 1, nomen = "Gaius", epistula = "g@roma.com", munus = "admin" }
    let int signum ← plenus.signum
    let Plenus reliqua ← plenus
    print signum
    print reliqua

    # --- Destructura from nested path ---

    let _ responsum ← Responsum { datum = Relatus { civis = Sodalis { nomen = "Claudia", probatus = true } } }
    let string internum ← responsum.datum.civis.nomen
    let bool probatus ← responsum.datum.civis.probatus
    print internum
    print probatus

    # --- Single field extraction ---

    let _ vestis ← Vestis { modus = "nigrum", lingua = "la" }
    let string modus ← vestis.modus
    print modus
}

Expected output:

Marcus
30
Julia
julia@roma.com
100
true
200
false
localis
8080
true
1
Plenus { signum = 1, nomen = "Gaius", epistula = "g@roma.com", munus = "admin" }
Claudia
true
nigrum