Kết xuấtvi

valor

Translation status: Tiếng Việt reader-locale proof. Code fences render through the vi pipeline; prose is canonical Latin.

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

Syntax: valor

Category#

conversion

Examples#

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

T ↦ valor boxing for scalar, byte, collection, genus, and tensor carriers.

# =============================================================================
# valor — T ↦ valor boxing for scalar, byte, collection, genus, and tensor carriers.
# =============================================================================
#
# What this teaches:
#   • Boxing into valor — `T ↦ valor` boxes any typed value (scalar, byte, collection, genus, tensor) into the canonical dynamic carrier.
#   • Byte preservation — `octeti ↦ valor` composes as numeric byte values; it must not lossy-decode bytes as text.
#
# Common mistakes:
#   • Expecting to extract a specific type from valor without a matching variant — valor requires the correct runtime variant or `⇥` recovery.
#
# See also: ↦, valor, octeti, lista, tabula, genus, tensor
# =============================================================================

# conversio — typed values to valor boxing
#
# WHY: `T ↦ valor` boxes typed values into the canonical dynamic carrier.
# `octeti` composes as a list of numeric byte values; it must not lossy-decode
# bytes as text.

class Punctum {
    int x
    int y
}

main {
    const int n  42
    const value scalar  n ↦ value
    print scalar

    const bytes bytes  |de ad|
    const value boxedBytes  bytes ↦ value
    print boxedBytes

    const list<int> xs  [1, 2]
    const value boxedList  xs ↦ value
    print boxedList

    const map<string, int> scores  { "alpha": 10 }
    const value boxedMap  scores ↦ value
    print boxedMap

    const Punctum pt  Punctum { x = 3, y = 4 }
    const value boxedGenus  pt ↦ value
    print boxedGenus

    const tensor<int, [2]> grid  [5, 6] ↦ tensor<int, [2]>
    const value boxedTensor  grid ↦ value
    print boxedTensor
}

Expected output:

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

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

valor ↦ genus extraction with missing-field defaults and mandatory-field failure via ⇥ recovery.

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

# conversio — valor to genus extraction
#
# WHY: `valor ↦ genus` deserializes JSON objects into typed records. Missing
# defaultable fields zero-initialize; missing mandatory fields fail at runtime
# (recoverable via `⇥`). Extra JSON keys are ignored.

class Persona {
    string nomen
    int aetas optional
    string regio = "Roma"
    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 valor ↦ T extraction via FromValor (variant match, fractus widen, textus|instans wire).

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

# conversio — scalar valor extraction
#
# WHY: `valor ↦ T` is the default runtime extraction for dynamic JSON carriers.
# Scalar arms match `Valor` variants; `valor ↦ fractus` widens `Numerus` losslessly;
# `valor ↦ textus` accepts `Textus` and `Instans` wire strings. Typed datetime
# provenance stays on `valor ↦ instans` (see conversio/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 valor ↔ tensor roundtrip via lista bridges (no dedicated valor↔tensor arms).

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

# conversio — valor ↔ tensor compose roundtrip
#
# WHY: `valor ↔ tensor` has no dedicated codegen arms. Extraction materializes
# `lista<T>` first; construction uses shipped `lista ↦ tensor` (`structa`) and
# flatten via `tensor ↦ lista` (`.planata()`), then `lista ↦ valor` boxing.

main {
    const value arr  [1, 2, 3, 4]
    const list<int> xs  arr ↦ list<int>
    assert xs.longitudo()  4

    const tensor<int, [4]> grid  xs ↦ tensor<int, [4]>
    const list<int> flat  grid ↦ list<int>
    assert flat.longitudo()  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.accipe("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.

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

# destructura — valor literal expressions (inline JSON)
#
# { "clavis": expr, ... }              -- JSON object literal (valor)
# { "clavis": expr, "nidum": { ... } } -- nested JSON objects
#
# Anonymous Faber object literals (`{ key = expr }`) are retired. Bare `{ }`
# now denotes a JSON valor 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 valor, point coords, keyed fields, nested records.
#
# BACKEND: Go emitter does not lower object `sparge` into maps (whitelist:
# destructura/literal.fab).

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

    const _ void  {}
    print void

    const _ punctum  { "x": 10, "y": 20 }
    print punctum

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

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

    # --- Nested valor ---

    const _ nidum  { "extra": { "medium": 1 } }
    print nidum
}