渲染zh-Hant

值

Translation status: 繁體中文 reader-locale proof. Term names and code fences follow the zh-Hant pack; supporting prose may still be English.

JSON-shaped dynamic value literal for open object-shaped data.

Syntax: 值

Category#

conversion

Examples#

radix/corpus/conversio/valor-boxing.fab (canonical · conversio)#

T ↦ 值 boxing for scalar, byte, collection, 類型, and tensor carriers.

# =============================================================================
# 值 — T ↦ 值 boxing for scalar, byte, collection, 類型, and 張量 carriers.
# =============================================================================
#
# What this teaches:
#   • Boxing into 值 — `T ↦ 值` boxes any typed value (scalar, byte, collection, 類型, 張量) into the canonical dynamic carrier.
#   • Byte preservation — `位元組 ↦ 值` composes as numeric byte values; it must not lossy-decode bytes as text.
#
# Common mistakes:
#   • Expecting to extract a specific type from 值 without a matching variant — 值 requires the correct runtime variant or `⇥` recovery.
#
# See also: ↦, 值, 位元組, 列表, 表格, 類型, 張量
# =============================================================================

# 轉換 — typed values to 值 boxing
#
# WHY: `T ↦ 值` boxes typed values into the canonical dynamic carrier.
# `位元組` composes as a list of numeric byte values; it must not lossy-decode
# bytes as text.
#
# A 值 object has no defined key order (DECISIONS D2.9), so the 類型 case
# reads its fields back instead of printing the object text.

class Punctum {
    var int x
    var int y
}

main {
    const int n ← 42
    const value scalar ← n ↦ value
    print scalar
    const bytes bytes ← |從 端點|
    const value boxedBytes ← bytes ↦ value
    print boxedBytes
    const list<int> numeri ← [1, 2]
    const value boxedList ← numeri ↦ value
    print boxedList
    const map<string, int> scores ← { "alpha": 10 }
    const value boxedMap ← scores ↦ value
    print boxedMap
    const Punctum punctum ← Punctum { x = 3, y = 4 }
    const value boxedGenus ← punctum ↦ value
    const Punctum unboxed ← boxedGenus ↦ Punctum
    print unboxed.x
    print unboxed.y
    const 張量<int, [2]> grid ← [5, 6] ↦ 張量<int, [2]>
    const value boxedTensor ← grid ↦ value
    print boxedTensor
}

Expected output:

42
[222, 173]
[1, 2]
{"alpha": 10}
3
4
[5, 6]

radix/corpus/conversio/valor-composita.fab (canonical · conversio)#

類型, 列表, 表格, and tensor behind a 值: boxing, 值 display, and 值 ↦ T round trips.

# =============================================================================
# 值 — 類型, 列表, 表格, and 張量 behind a 值
# =============================================================================
#
# What this teaches:
#   • Boxing — `T ↦ 值` keeps the composite itself behind the dynamic carrier; nothing is copied or converted.
#   • Display — a 值 prints JSON-shaped whatever it holds: text unquoted, map keys quoted, a 類型 as an object in declaration order.
#   • Extraction — `值 ↦ T` takes the composite back out: a 類型 by its own type, a 列表 or 表格 element by element.
#
# Common mistakes:
#   • Expecting `值 ↦ 張量` to return a boxed 張量 — a 張量 behind a 值 prints but never extracts; compose through `列表<T>` (see valor-tensor).
#   • Printing a map with several keys and pinning its order — 表格 order is unspecified, so this file boxes a single key.
#
# See also: ↦, 值, 列表, 表格, 類型, 張量
# =============================================================================

# 轉換 — composites behind a 值
#
# WHY: `值` is the dynamic carrier, so the runner keeps a composite as it is
# behind one and displays it as JSON. This pins the display and the round trip
# for each flat composite family, separately from the scalar rows
# (valor-scalaria), the byte row (valor-boxing) and the deeper rows — nested
# 類型, widening, recovery and tag tests (valor-composita-deep).

class Punctum {
    var int x
    var int y
}

main {
    # 1. A 類型: the object prints in field order and comes back with its fields.
    const Punctum punctum ← Punctum { x = 3, y = 4 }
    const value boxedGenus ← punctum ↦ value
    print boxedGenus
    const Punctum unboxed ← boxedGenus ↦ Punctum
    print unboxed.x
    print unboxed.y

    # 2. A 列表: elements keep their type, so text is unquoted behind the 值 and quoted again after.
    const list<int> numeri ← [1, 2]
    const value boxedNumeri ← numeri ↦ value
    print boxedNumeri
    const list<int> backNumeri ← boxedNumeri ↦ list<int>
    print backNumeri
    const list<string> verba ← ["a", "b"]
    const value boxedVerba ← verba ↦ value
    print boxedVerba
    const list<string> backVerba ← boxedVerba ↦ list<string>
    print backVerba

    # 3. A 表格: keys print quoted, and the map comes back key for key.
    const map<string, int> scores ← { "alpha": 10 }
    const value boxedScores ← scores ↦ value
    print boxedScores
    const map<string, int> backScores ← boxedScores ↦ map<string, int>
    print backScores.get("alpha") coalesce 0

    # 4. A 張量: it prints flat.
    const 張量<int, [2]> grid ← [5, 6] ↦ 張量<int, [2]>
    const value boxedGrid ← grid ↦ value
    print boxedGrid
}

Expected output:

{"x": 3, "y": 4}
3
4
[1, 2]
[1, 2]
[a, b]
["a", "b"]
{"alpha": 10}
10
[5, 6]

radix/corpus/conversio/valor-genus.fab (canonical · conversio)#

值 ↦ 類型 extraction with missing-field defaults and mandatory-field failure via ⊥ recovery.

# =============================================================================
# 值 — 值 ↦ 類型 extraction with missing-field defaults and mandatory-field failure via ⊥ recovery.
# =============================================================================
#
# What this teaches:
#   • JSON-to-struct deserialization — `值 ↦ 類型` deserializes JSON objects into typed 類型 records.
#   • Field policies — missing `可選` fields zero-initialize; missing mandatory fields fail at runtime (recoverable via a `⊥` default); extra JSON keys are ignored.
#
# Common mistakes:
#   • Omitting a mandatory field from the JSON and expecting a default value — only `可選` or default-valued fields are optional; missing mandatory fields fail at runtime.
#
# See also: ↦, ⊥
# =============================================================================

# 轉換 — 值 to 類型 extraction
#
# WHY: `值 ↦ 類型` deserializes JSON objects into typed records. Missing
# defaultable fields zero-initialize; missing mandatory fields fail at runtime
# (recoverable via a `⊥` default). Extra JSON keys are ignored.

class Persona {
    var string 名稱
    var int aetas optional
    var string 模組 = "Roma"
    var instant born
}

main {
    const value payload ← { "nomen": "Marcus", "born": "1979-05-27T07:32:00Z", "extra": "ignored" } ↦ value
    const Persona good ← payload ↦ Persona
    assert good.nomen ≡ "Marcus"
    assert good.aetas ≡ null
    assert good.regio ≡ "Roma"
    const value stale ← { "nomen": "Livia" } ↦ value
    const Persona recovered ← stale ↦ Persona ⊥ good
    assert recovered.nomen ≡ "Marcus"
    const value boxed ← recovered ↦ value
    const Persona roundtrip ← boxed ↦ Persona
    assert roundtrip.nomen ≡ "Marcus"
    assert roundtrip.regio ≡ "Roma"
    print roundtrip.nomen
    print roundtrip.regio
    print roundtrip.born ↦ string
}

Expected output:

Marcus
Roma
1979-05-27T07:32:00Z

radix/corpus/conversio/valor-scalaria.fab (canonical · conversio)#

Scalar 值 ↦ T extraction via FromValor (variant match, 小數 widen, 文字|執行個體 wire).

# =============================================================================
# 值 — Scalar 值 ↦ T extraction via FromValor (variant match, 小數 widen, 文字|執行個體 wire).
# =============================================================================
#
# What this teaches:
#   • Scalar extraction — `值 ↦ T` extracts typed values from dynamic JSON carriers using variant matching.
#   • Widening — `值 ↦ 小數` widens `Numerus` variants losslessly.
#   • Wire text — `值 ↦ 文字` accepts both `Textus` and `Instans` wire strings.
#   • Recovery — `⇥` provides a fallback on variant mismatch.
#
# Common mistakes:
#   • Assuming `值 ↦ 文字` works for non-text 值 variants — only Textus and Instans wire strings are accepted; other variants fail without a `⊥` default.
#
# See also: ↦, ⊥, 執行個體, 文字
# =============================================================================

# 轉換 — scalar 值 extraction
#
# WHY: `值 ↦ T` is the default runtime extraction for dynamic JSON carriers.
# Scalar arms match `Valor` variants; `值 ↦ 小數` widens `Numerus` losslessly;
# `值 ↦ 文字` accepts `Textus` and `Instans` wire strings. Typed datetime
# provenance stays on `值 ↦ 執行個體` (see 轉換/instans.fab). `⊥`
# recovers on variant mismatch.

main {
    const value asInt ← 42
    const int count ← asInt ↦ int
    assert count ≡ 42
    const value asFloat ← 3.5
    const float ratio ← asFloat ↦ float
    assert ratio ≡ 3.5
    const value intForFloat ← 7
    const float widened ← intForFloat ↦ float
    assert widened ≡ 7.0
    const value asBool ← true
    const bool flag ← asBool ↦ bool
    assert flag ≡ true
    const value asText ← "salve"
    const string greeting ← asText ↦ string
    assert greeting ≡ "salve"
    const value asWire ← "1979-05-27T07:32:00Z"
    const string wire ← asWire ↦ string
    assert wire ≡ "1979-05-27T07:32:00Z"
    const value asAscii ← 'yes'
    const ascii token ← asAscii ↦ ascii
    assert token ≡ 'yes'
    const value bad ← "not-a-number"
    const int recovered ← bad ↦ int ⊥ 0
    assert recovered ≡ 0
    print count
    print ratio
    print widened
    print flag
    print greeting
    print wire
    print token
    print recovered
}

Expected output:

42
3.5
7.0
verum
salve
1979-05-27T07:32:00Z
yes
0

radix/corpus/conversio/valor-tensor.fab (canonical · conversio)#

Compose 值 ↔ tensor roundtrip via 列表 bridges (no dedicated 值↔tensor arms).

# =============================================================================
# 值 — Compose 值 ↔ 張量 roundtrip via 列表 bridges (no dedicated 值↔張量 arms).
# =============================================================================
#
# What this teaches:
#   • Indirect 張量 變換 — `值 ↔ 張量` has no dedicated codegen arms; it materializes through `列表<T>` as an intermediate step.
#   • Roundtrip path — 值 → 列表 → 張量 → 列表 → 值, showing composition of existing operators.
#
# Common mistakes:
#   • Expecting `值 ↦ 張量` to work directly without the 列表 bridge — there are no dedicated 值↔張量 arms; compose through `列表<T>` instead.
#
# See also: ↦, 值, 列表, 張量
# =============================================================================

# 轉換 — 值 ↔ 張量 compose roundtrip
#
# WHY: `值 ↔ 張量` has no dedicated codegen arms. Extraction materializes
# `列表<T>` first; construction uses shipped `列表 ↦ 張量` (`structa`) and
# flatten via `張量 ↦ 列表` (`.flatten()`), then `列表 ↦ 值` boxing.

main {
    const value arr ← [1, 2, 3, 4]
    const list<int> xs ← arr ↦ list<int>
    assert xs.length() ≡ 4
    const 張量<int, [4]> grid ← xs ↦ 張量<int, [4]>
    const list<int> flat ← grid ↦ list<int>
    assert flat.length() ≡ 4
    const value roundtrip ← flat ↦ value
    print roundtrip
    const value obj ← { "alpha": 10, "beta": 20 } ↦ value
    const map<string, int> scores ← obj ↦ map<string, int>
    print scores.get("alpha") coalesce 0
}

Expected output:

[1, 2, 3, 4]
10

radix/corpus/destructura/literal.fab (canonical · existing-home)#

JSON-shaped dynamic value literal for open object-shaped data.

# =============================================================================
# 值 — JSON-shaped dynamic value literal for open object-shaped data.
# =============================================================================
#
# What this teaches:
#   • JSON 值 literals — `{ "key": value, ... }` creates dynamic 值 objects using JSON5 syntax with trailing commas.
#   • Nested objects — JSON 值 literals can be nested for complex data shapes.
#   • Constraint — JSON 值 literals accept only constant values (strings, numbers, booleans, null, nested objects/arrays); use 類型 or 表格 for variable-backed construction.
#
# Common mistakes:
#   • Using retired bare object literals `{ key = expr }` instead of 類型 construction `Type { field = expr }` — bare `{ }` is now a JSON 值 literal with quoted keys and `:` separator.
#
# See also: 類型, ∷, 未知, 表格
# =============================================================================

# destructura — 值 literal expressions (inline JSON)
#
# { "clavis": expr, ... }              -- JSON object literal (值)
# { "clavis": expr, "nidum": { ... } } -- nested JSON objects
#
# Anonymous Faber object literals (`{ key = expr }`) are retired. Bare `{ }`
# now denotes a JSON 值 literal: keys are quoted JSON strings separated by
# `:`, values are JSON scalars, arrays, or nested objects. Trailing commas are
# permitted (JSON5-style). Duplicate keys are an error (second occurrence).
#
# GRAMMAR:
#   jsonLiteral :← '{' (jsonMember (',' jsonMember)* ','?)? '}'
#   jsonMember  :← string ':' jsonValue
#
# EXPECTED OUTPUT:
#   Empty 值, point coords, keyed fields, nested records.
#
# BACKEND: Go emitter does not lower object `展開` into maps (whitelist:
# destructura/literal.fab).

main {
    # --- Empty and simple records ---

    const JSON void ← {}
    print void
    const JSON punctum ← { "x": 10, "y": 20 }
    print punctum

    # Text keys and constant values
    const JSON forma ← { "clavis": 42 }
    print forma

    # NOTE: JSON 值 literals accept only constant values (strings, numbers,
    # booleans, null, nested objects/arrays). For variable-backed field
    # construction, use 類型 `Type { field = expr }` or 表格 insertion.

    # --- Nested 值 ---

    const JSON nidum ← { "extra": { "medium": 1 } }
    print nidum
}