การเรนเดอร์th-TH

ออก

Translation status: ภาษาไทย reader-locale proof. Term names and code fences follow the th-TH pack; supporting prose may still be English.

Extracts fields from a value into local bindings.

Syntax: ออก <expression> <คงที่|แปร> <field> [, ...] [ที่เหลือ <ident>]

Category#

destructuring

Examples#

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

Extracts fields from a value into local bindings.

# =============================================================================
# ออก — Extracts fields from a value into local bindings.
# =============================================================================
#
# What this teaches:
#   • Lista destructuring — `คงที่ [a, b, c] ← รายการ` extracts elements from a รายการ into individual bindings.
#   • Rest patterns — `คงที่ [caput, ที่เหลือ cauda] ← รายการ` captures remaining elements.
#   • Mutable destructuring — `แปร [x, y] ← รายการ` for mutable bindings.
#   • Skip and nested patterns — underscore `_` skips elements; nested listas destructure recursively.
#
# Common mistakes:
#   • Destructuring fewer or more elements than the รายการ contains without handling bounds — pattern must match or the destructuring will fail.
#
# See also: จาก, ในชื่อ, ที่เหลือ
# =============================================================================


# destructura — รายการ destructuring patterns
#
# คงที่ [a, b, c] ← รายการ
# แปร [x, y, z] ← รายการ
# คงที่ [caput, ที่เหลือ cauda] ← รายการ
# คงที่ [_, medius, _] ← รายการ
#
# GRAMMAR:
#   destructStmt :← ('คงที่' | 'แปร') '[' patternList ']' '←' expr
#
# EXPECTED OUTPUT:
#   Element values from inline and bound listas, rest slices, nested rows.

main {
    # --- Basic รายการ destructuring ---

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

    # Destructure inline รายการ literal
    const [primus, secundus, tertius] ← [10, 20, 30]
    print primus
    print secundus
    print tertius

    # --- Mutable destructuring with แปร ---

    const _ coordinata ← [100, 200]
    var [x, y] ← coordinata
    print x
    print y
    x ← x + 50
    y ← y + 50
    print x
    print y

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

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

    # --- Rest pattern with ที่เหลือ ---

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

    # --- Skip elements with underscore ---

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

    # --- Nested listas ---

    const _ เมทริกซ์ ← [[1, 2], [3, 4]]
    const [ordo1, ordo2] ← เมทริกซ์
    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/destructura/objectum.fab (canonical · keyword)#

Extracts fields from a value into local bindings.

# =============================================================================
# ออก — Extracts fields from a value into local bindings.
# =============================================================================
#
# What this teaches:
#   • Field destructuring — `ออก <expr> คงที่ <field>, ...` extracts named fields from ชนิด records into local bindings.
#   • Aliasing — `ออก <expr> คงที่ <field> ในชื่อ <alias>` renames extracted fields.
#   • Mutable extraction — `ออก <expr> แปร <field>, ...` for mutable field bindings.
#   • Rest pattern — `ออก <expr> คงที่ <field>, ที่เหลือ <rest>` captures remaining fields as a ตาราง.
#   • Nested paths — `ออก <expr>.<field> คงที่ ...` destructures through nested paths.
#
# Common mistakes:
#   • Destructuring a field that does not exist on the target type — the field must be present in the ชนิด definition or the type must support open shapes.
#
# See also: จาก, ในชื่อ, ที่เหลือ
# =============================================================================


# destructura — field destructuring with `ออก`
#
# ออก <expr> คงที่ <field>, ...
# ออก <expr> แปร <field>, ...
# ออก <expr> คงที่ <field> ในชื่อ <alias>
# ออก <expr> คงที่ <field>, ที่เหลือ <rest>
#
# Field destructuring works on ชนิด records (fixed fields) and ตาราง/valor
# (open shapes). This tour uses ชนิด types so that field names are statically
# known and codegen-complete.
#
# GRAMMAR:
#   destructStmt :← 'ออก' expr ('คงที่' | 'แปร') patternList
#
# EXPECTED OUTPUT:
#   Extracted field values, aliased names, mutated แปร bindings, rest maps.

class Persona {
    var string ชื่อ
    var int aetas
    var string urbs
}

class Civis {
    var string ชื่อ
    var string epistula
}

class Datum {
    var int จํานวน
    var bool activus
}

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

class Plenus {
    var int signum
    var string ชื่อ
    var string epistula
    var string munus
}

class Sodalis {
    var string ชื่อ
    var bool probatus
}

class Relatus {
    var Sodalis civis
}

class Responsum {
    var Relatus datum
}

class Vestis {
    var string modus
    var string lingua
}

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

    const _ persona ← Persona { ชื่อ = "Marcus", aetas = 30, urbs = "Roma" }
    from persona const ชื่อ, aetas

    print ชื่อ
    print aetas

    # --- Extract with alias using 'ในชื่อ' ---

    const _ civis ← Civis { ชื่อ = "Julia", epistula = "julia@roma.com" }
    from civis const ชื่อ as cognomen, epistula as epistula2

    print cognomen
    print epistula2

    # --- Mutable destructuring with แปร ---

    const _ datum ← Datum { จํานวน = 100, activus = true }
    from datum var จํานวน, activus

    print จํานวน
    print activus

    จํานวน ← 200
    activus ← false

    print จํานวน
    print activus

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

    const _ forma ← Forma { hospes = "localis", portus = 8080, tutus = true }
    from forma const hospes, portus as porta, tutus

    print hospes
    print porta
    print tutus

    # --- Rest pattern with ที่เหลือ ---

    const _ plenus ← Plenus { signum = 1, ชื่อ = "Gaius", epistula = "g@roma.com", munus = "admin" }
    from plenus const signum, rest reliqua

    print signum
    print reliqua

    # --- Destructura ออก nested path ---

    const _ responsum ← Responsum { datum = Relatus { civis = Sodalis { ชื่อ = "Claudia", probatus = true } } }
    from responsum.datum.civis const ชื่อ as internum, probatus

    print internum
    print probatus

    # --- Single field extraction ---

    const _ vestis ← Vestis { modus = "nigrum", lingua = "la" }
    from vestis const modus

    print modus
}

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

Extracts fields from a value into local bindings.

# =============================================================================
# ออก — Extracts fields from a value into local bindings.
# =============================================================================
#
# What this teaches:
#   • Field extraction — `ออก <expr> คงที่ <field>, ...` extracts named fields from ชนิด records into local scope.
#   • Multiple field extraction — extract several fields from a single expression in one statement.
#
# Common mistakes:
#   • Mode mismatch — passing a `จาก` (read-only) parameter to an `ออก` (consume) position, or extracting a field that doesn't exist on the target type.
#
# See also: จาก, ในชื่อ, ที่เหลือ
# =============================================================================

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

class Persona {
    var string ชื่อ
    var int aetas
}

main {
    const _ p ← Persona { ชื่อ = "Marcus", aetas = 30 }
    from p const ชื่อ, aetas
    print ชื่อ
    print aetas
}

Expected output:

Marcus
30

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

Extracts fields from a value into local bindings.

# =============================================================================
# ออก — Extracts fields from a value into local bindings.
# =============================================================================
#
# What this teaches:
#   • Loop-binder destructuring — `for from xs const [a, b] { ... }` binds each
#     element through the same pattern grammar as binding-position forms.
#   • Records (ชนิด) — `for from puncta const {x, y} { ... }` object_pattern
#     binds fields by name on each element.
#   • Positional tuples — `for from ordines const [a, b] { ... }` binds tuple
#     slots positionally.
#   • Rest patterns — `for from matricula const [caput, rest cauda]` captures
#     the remaining รายการ.
#   • Holes — `for from ordines const [_, 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: จาก, ในชื่อ, ที่เหลือ
# =============================================================================

# วน — destructuring loop binders
#
# for from <collection> <const|var> <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 :← 'วน' 'ออก' expr ('คงที่' | 'แปร') pattern block
#
# EXPECTED OUTPUT:
#   destructuring.expected — sums and elements from every pattern form.

class Punctum {
    var int x
    var int y
}

main {
    # --- Records: object_pattern over รายการ of ชนิด elements ---

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

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

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

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

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

    # --- Rest patterns: first element plus the remaining รายการ ---

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

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

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

Expected output:

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

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

Extracts fields from a value into local bindings.

# =============================================================================
# ออก — Extracts fields from a value into local bindings.
# =============================================================================
#
# What this teaches:
#   • value iteration (`วน ออก`) over รายการ elements — the primary workhorse for iterating over collections
#   • transforming values inside the loop body (e.g., doubling each element)
#
# Common mistakes:
#   • Using ออก (value iteration) when จาก (key/index iteration) is needed — ออก iterates values, จาก iterates keys or indices.
#
# See also: จาก, ในชื่อ, ที่เหลือ
# =============================================================================

# วน ออก — for-in value iteration over listas
#
# วน ออก <collection> คงที่ <item> { <body> }
#
# GRAMMAR:
#   forInStmt :← 'วน' 'ออก' expr 'คงที่' ident block
#
# EXPECTED OUTPUT:
#   ex.expected — numbers, names, and doubled values.

main {
    # Iterate over number รายการ
    const list<int, 5> numeri ← [1, 2, 3, 4, 5]
    for from numeri const n {
        print n
    }

    # Iterate over ข้อความ รายการ
    const list<string, 3> nomina ← ["Marcus", "Julia", "Claudia"]
    for from nomina const ชื่อ {
        print ชื่อ
    }

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

Expected output:

1
2
3
4
5
Marcus
Julia
Claudia
20
40
60