giá_trị
Translation status: Tiếng Việt reader-locale proof. Term names and code fences follow the vi pack; supporting prose may still be English.
JSON-shaped dynamic value literal for open object-shaped data.
Syntax: giá_trị
Category#
conversion
Related#
Examples#
radix/corpus/conversio/valor-boxing.fab (canonical · conversio)#
T ↦ giá_trị boxing for scalar, byte, collection, kiểu, and tensor carriers.
# =============================================================================
# giá_trị — T ↦ giá_trị boxing for scalar, byte, collection, kiểu, and ten_xo carriers.
# =============================================================================
#
# What this teaches:
# • Boxing into giá_trị — `T ↦ giá_trị` boxes any typed value (scalar, byte, collection, kiểu, ten_xo) into the canonical dynamic carrier.
# • Byte preservation — `byte ↦ giá_trị` composes as numeric byte values; it must not lossy-decode bytes as text.
#
# Common mistakes:
# • Expecting to extract a specific type from giá_trị without a matching variant — giá_trị requires the correct runtime variant or `⇥` recovery.
#
# See also: ↦, giá_trị, byte, danh_sách, bảng, kiểu, ten_xo
# =============================================================================
# chuyển_đổi — typed values to giá_trị boxing
#
# WHY: `T ↦ giá_trị` boxes typed values into the canonical dynamic carrier.
# `byte` composes as a list of numeric byte values; it must not lossy-decode
# bytes as text.
#
# A giá_trị object has no defined key order (DECISIONS D2.9), so the kiểu 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 ← |ra gọi|
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 ten_xo<int, [2]> grid ← [5, 6] ↦ ten_xo<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)#
kiểu, danh_sách, bảng, and tensor behind a giá_trị: boxing, giá_trị display, and giá_trị ↦ T round trips.
# =============================================================================
# giá_trị — kiểu, danh_sách, bảng, and ten_xo behind a giá_trị
# =============================================================================
#
# What this teaches:
# • Boxing — `T ↦ giá_trị` keeps the composite itself behind the dynamic carrier; nothing is copied or converted.
# • Display — a giá_trị prints JSON-shaped whatever it holds: text unquoted, map keys quoted, a kiểu as an object in declaration order.
# • Extraction — `giá_trị ↦ T` takes the composite back out: a kiểu by its own type, a danh_sách or bảng element by element.
#
# Common mistakes:
# • Expecting `giá_trị ↦ ten_xo` to return a boxed ten_xo — a ten_xo behind a giá_trị prints but never extracts; compose through `danh_sách<T>` (see valor-tensor).
# • Printing a map with several keys and pinning its order — bảng order is unspecified, so this file boxes a single key.
#
# See also: ↦, giá_trị, danh_sách, bảng, kiểu, ten_xo
# =============================================================================
# chuyển_đổi — composites behind a giá_trị
#
# WHY: `giá_trị` 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
# kiểu, widening, recovery and tag tests (valor-composita-deep).
class Punctum {
var int x
var int y
}
main {
# 1. A kiểu: 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 danh_sách: elements keep their type, so text is unquoted behind the giá_trị 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 bảng: 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 ten_xo: it prints flat.
const ten_xo<int, [2]> grid ← [5, 6] ↦ ten_xo<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)#
giá_trị ↦ kiểu extraction with missing-field defaults and mandatory-field failure via ⊥ recovery.
# =============================================================================
# giá_trị — giá_trị ↦ kiểu extraction with missing-field defaults and mandatory-field failure via ⊥ recovery.
# =============================================================================
#
# What this teaches:
# • JSON-to-struct deserialization — `giá_trị ↦ kiểu` deserializes JSON objects into typed kiểu records.
# • Field policies — missing `tự_nguyện` 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 `tự_nguyện` or default-valued fields are optional; missing mandatory fields fail at runtime.
#
# See also: ↦, ⊥
# =============================================================================
# chuyển_đổi — giá_trị to kiểu extraction
#
# WHY: `giá_trị ↦ kiểu` 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 tên
var int aetas optional
var string vùng = "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 giá_trị ↦ T extraction via FromValor (variant match, thập_phân widen, văn_bản|thời_điểm wire).
# =============================================================================
# giá_trị — Scalar giá_trị ↦ T extraction via FromValor (variant match, thập_phân widen, văn_bản|thời_điểm wire).
# =============================================================================
#
# What this teaches:
# • Scalar extraction — `giá_trị ↦ T` extracts typed values from dynamic JSON carriers using variant matching.
# • Widening — `giá_trị ↦ thập_phân` widens `Numerus` variants losslessly.
# • Wire text — `giá_trị ↦ văn_bản` accepts both `Textus` and `Instans` wire strings.
# • Recovery — `⇥` provides a fallback on variant mismatch.
#
# Common mistakes:
# • Assuming `giá_trị ↦ văn_bản` works for non-text giá_trị variants — only Textus and Instans wire strings are accepted; other variants fail without a `⊥` default.
#
# See also: ↦, ⊥, thời_điểm, văn_bản
# =============================================================================
# chuyển_đổi — scalar giá_trị extraction
#
# WHY: `giá_trị ↦ T` is the default runtime extraction for dynamic JSON carriers.
# Scalar arms match `Valor` variants; `giá_trị ↦ thập_phân` widens `Numerus` losslessly;
# `giá_trị ↦ văn_bản` accepts `Textus` and `Instans` wire strings. Typed datetime
# provenance stays on `giá_trị ↦ thời_điểm` (see chuyển_đổi/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 giá_trị ↔ tensor roundtrip via danh_sách bridges (no dedicated giá_trị↔tensor arms).
# =============================================================================
# giá_trị — Compose giá_trị ↔ ten_xo roundtrip via danh_sách bridges (no dedicated giá_trị↔ten_xo arms).
# =============================================================================
#
# What this teaches:
# • Indirect ten_xo chuyển — `giá_trị ↔ ten_xo` has no dedicated codegen arms; it materializes through `danh_sách<T>` as an intermediate step.
# • Roundtrip path — giá_trị → danh_sách → ten_xo → danh_sách → giá_trị, showing composition of existing operators.
#
# Common mistakes:
# • Expecting `giá_trị ↦ ten_xo` to work directly without the danh_sách bridge — there are no dedicated giá_trị↔ten_xo arms; compose through `danh_sách<T>` instead.
#
# See also: ↦, giá_trị, danh_sách, ten_xo
# =============================================================================
# chuyển_đổi — giá_trị ↔ ten_xo compose roundtrip
#
# WHY: `giá_trị ↔ ten_xo` has no dedicated codegen arms. Extraction materializes
# `danh_sách<T>` first; construction uses shipped `danh_sách ↦ ten_xo` (`structa`) and
# flatten via `ten_xo ↦ danh_sách` (`.flatten()`), then `danh_sách ↦ giá_trị` boxing.
main {
const value arr ← [1, 2, 3, 4]
const list<int> xs ← arr ↦ list<int>
assert xs.length() ≡ 4
const ten_xo<int, [4]> grid ← xs ↦ ten_xo<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.
# =============================================================================
# giá_trị — JSON-shaped dynamic value literal for open object-shaped data.
# =============================================================================
#
# What this teaches:
# • JSON giá_trị literals — `{ "key": value, ... }` creates dynamic giá_trị objects using JSON5 syntax with trailing commas.
# • Nested objects — JSON giá_trị literals can be nested for complex data shapes.
# • Constraint — JSON giá_trị literals accept only constant values (strings, numbers, booleans, null, nested objects/arrays); use kiểu or bảng for variable-backed construction.
#
# Common mistakes:
# • Using retired bare object literals `{ key = expr }` instead of kiểu construction `Type { field = expr }` — bare `{ }` is now a JSON giá_trị literal with quoted keys and `:` separator.
#
# See also: kiểu, ∷, chưa_biết, bảng
# =============================================================================
# destructura — giá_trị literal expressions (inline JSON)
#
# { "clavis": expr, ... } -- JSON object literal (giá_trị)
# { "clavis": expr, "nidum": { ... } } -- nested JSON objects
#
# Anonymous Faber object literals (`{ key = expr }`) are retired. Bare `{ }`
# now denotes a JSON giá_trị 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 giá_trị, point coords, keyed fields, nested records.
#
# BACKEND: Go emitter does not lower object `rải` 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 giá_trị literals accept only constant values (strings, numbers,
# booleans, null, nested objects/arrays). For variable-backed field
# construction, use kiểu `Type { field = expr }` or bảng insertion.
# --- Nested giá_trị ---
const json nidum ← { "extra": { "medium": 1 } }
print nidum
}