value
Translation status: English reader-locale proof. Term names and code fences follow the en pack; supporting prose may still be English.
JSON-shaped dynamic value literal for open object-shaped data.
Syntax: value
Category#
conversion
Related#
Examples#
radix/corpus/reference/conversio/valor/valor-boxing.fab (canonical · conversio)#
T ↦ value boxing for scalar, byte, collection, class, and tensor carriers.
# =============================================================================
# value — T ↦ value boxing for scalar, byte, collection, class, and tensor carriers.
# =============================================================================
#
# What this teaches:
# • Boxing into value — `T ↦ value` boxes any typed value (scalar, byte, collection, class, tensor) into the canonical dynamic carrier.
# • Byte preservation — `list<b8> ↦ value` composes as numeric byte values; it must not lossy-decode bytes as text.
#
# Common mistakes:
# • Expecting to extract a specific type from value without a matching variant — value requires the correct runtime variant or `⇥` recovery.
#
# See also: ↦, value, list<b8>, list, map, class, tensor
# =============================================================================
# conversion — typed values to value boxing
#
# WHY: `T ↦ value` boxes typed values into the canonical dynamic carrier.
# `list<b8>` composes as a list of numeric byte values; it must not lossy-decode
# bytes as text.
#
# A value object has no defined key order (DECISIONS D2.9), so the class case
# reads its fields back instead of printing the object text.
class Punctum {
mut int x
mut int y
}
main {
let int n ← 42
let value scalar ← n ↦ value
print scalar
let list<b8> bytes ← |ref call|
let value boxedBytes ← bytes ↦ value
print boxedBytes
let list<int> numeri ← [1, 2]
let value boxedList ← numeri ↦ value
print boxedList
let map<string, int> scores ← { "alpha": 10 }
let value boxedMap ← scores ↦ value
print boxedMap
let Punctum punctum ← Punctum { x = 3, y = 4 }
let value boxedGenus ← punctum ↦ value
let Punctum unboxed ← boxedGenus ↦ Punctum
print unboxed.x
print unboxed.y
let tensor<int, [2]> grid ← [5, 6] ↦ tensor<int, [2]>
let value boxedTensor ← grid ↦ value
print boxedTensor
}Expected output:
42
[222, 173]
[1, 2]
{"alpha": 10}
3
4
[5, 6]
radix/corpus/reference/conversio/valor/valor-composita.fab (canonical · conversio)#
class, list, map, and tensor behind a value: boxing, value display, and value ↦ T round trips.
# =============================================================================
# value — class, list, map, and tensor behind a value
# =============================================================================
#
# What this teaches:
# • Boxing — `T ↦ value` keeps the composite itself behind the dynamic carrier; nothing is copied or converted.
# • Display — a value prints JSON-shaped whatever it holds: text unquoted, map keys quoted, a class as an object in declaration order.
# • Extraction — `value ↦ T` takes the composite back out: a class by its own type, a list or map element by element.
#
# Common mistakes:
# • Expecting `value ↦ tensor` to return a boxed tensor — a tensor behind a value prints but never extracts; compose through `list<T>` (see valor-tensor).
# • Printing a map with several keys and pinning its order — map order is unspecified, so this file boxes a single key.
#
# See also: ↦, value, list, map, class, tensor
# =============================================================================
# conversion — composites behind a value
#
# WHY: `value` 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
# class, widening, recovery and tag tests (valor-composita-deep).
# The fields are declared y then x; the boxed object prints declaration order,
# not the order the literal below writes them.
class Punctum {
mut int y
mut int x
}
main {
# 1. A class: the object prints in field order and comes back with its fields.
let Punctum punctum ← Punctum { x = 3, y = 4 }
let value boxedGenus ← punctum ↦ value
print boxedGenus
let Punctum unboxed ← boxedGenus ↦ Punctum
print unboxed.x
print unboxed.y
# 2. A list: elements keep their type, so text is unquoted behind the value and quoted again after.
let list<int> numeri ← [1, 2]
let value boxedNumeri ← numeri ↦ value
print boxedNumeri
let list<int> backNumeri ← boxedNumeri ↦ list<int>
print backNumeri
let list<string> verba ← ["a", "b"]
let value boxedVerba ← verba ↦ value
print boxedVerba
let list<string> backVerba ← boxedVerba ↦ list<string>
print backVerba
# 3. A map: keys print quoted, and the map comes back key for key.
let map<string, int> scores ← { "alpha": 10 }
let value boxedScores ← scores ↦ value
print boxedScores
let map<string, int> backScores ← boxedScores ↦ map<string, int>
print backScores.get("alpha") coalesce 0
# 4. A tensor: it prints flat.
let tensor<int, [2]> grid ← [5, 6] ↦ tensor<int, [2]>
let value boxedGrid ← grid ↦ value
print boxedGrid
}Expected output:
{"y": 4, "x": 3}
3
4
[1, 2]
[1, 2]
[a, b]
["a", "b"]
{"alpha": 10}
10
[5, 6]
radix/corpus/reference/conversio/valor/valor-genus.fab (canonical · conversio)#
value ↦ class extraction with missing-field defaults and mandatory-field failure via ⊥ recovery.
# =============================================================================
# value — value ↦ class extraction with missing-field defaults and mandatory-field failure via ⊥ recovery.
# =============================================================================
#
# What this teaches:
# • JSON-to-struct deserialization — `value ↦ class` deserializes JSON objects into typed class records.
# • Field policies — missing `optional` 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 `optional` or default-valued fields are optional; missing mandatory fields fail at runtime.
#
# See also: ↦, ⊥
# =============================================================================
# conversion — value to class extraction
#
# WHY: `value ↦ class` 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.
type Count = int
type Total = Count
type Label = string
type Flag = bool
class AliasDefaults {
mut Total count
mut Label label
mut Flag active
}
class Persona {
mut string name
mut int aetas optional
mut string regio = "Roma"
mut instant born
mut string praenomen
mut ascii signum
mut int score
mut float record
mut bool activus
}
main {
let value payload ← { "nomen": "Marcus", "born": "1979-05-27T07:32:00Z", "extra": "ignored" } ↦ value
let Persona good ← payload ↦ Persona
assert good.nomen ≡ "Marcus"
assert good.aetas ≡ null
assert good.regio ≡ "Roma"
# None of these keys is present; aliases keep their underlying natural zero.
let AliasDefaults defaults ← payload ↦ AliasDefaults
assert defaults.count ≡ 0
assert defaults.label ≡ ""
assert defaults.active ≡ false
let value stale ← { "nomen": "Livia" } ↦ value
let Persona recovered ← stale ↦ Persona ⊥ good
assert recovered.nomen ≡ "Marcus"
let value boxed ← recovered ↦ value
let 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/reference/conversio/valor/valor-scalaria.fab (canonical · conversio)#
Scalar value ↦ T extraction via FromValor (variant match, float widen, string|instant wire).
# =============================================================================
# value — Scalar value ↦ T extraction via FromValor (variant match, float widen, string|instant wire).
# =============================================================================
#
# What this teaches:
# • Scalar extraction — `value ↦ T` extracts typed values from dynamic JSON carriers using variant matching.
# • Widening — `value ↦ float` widens `Numerus` variants losslessly.
# • Wire text — `value ↦ string` accepts both `Textus` and `Instans` wire strings.
# • Recovery — `⇥` provides a fallback on variant mismatch.
#
# Common mistakes:
# • Assuming `value ↦ string` works for non-text value variants — only Textus and Instans wire strings are accepted; other variants fail without a `⊥` default.
#
# See also: ↦, ⊥, instant, string
# =============================================================================
# conversion — scalar value extraction
#
# WHY: `value ↦ T` is the default runtime extraction for dynamic JSON carriers.
# Scalar arms match `Valor` variants; `value ↦ float` widens `Numerus` losslessly;
# `value ↦ string` accepts `Textus` and `Instans` wire strings. Typed datetime
# provenance stays on `value ↦ instant` (see conversion/instans.fab). `⊥`
# recovers on variant mismatch.
main {
let value asInt ← 42
let int count ← asInt ↦ int
assert count ≡ 42
let value asFloat ← 3.5
let float record ← asFloat ↦ float
assert record ≡ 3.5
let value intForFloat ← 7
let float widened ← intForFloat ↦ float
assert widened ≡ 7.0
let value asBool ← true
let bool flag ← asBool ↦ bool
assert flag ≡ true
let value asText ← "salve"
let string greeting ← asText ↦ string
assert greeting ≡ "salve"
let value asWire ← "1979-05-27T07:32:00Z"
let string wire ← asWire ↦ string
assert wire ≡ "1979-05-27T07:32:00Z"
let value asAscii ← 'yes'
let ascii token ← asAscii ↦ ascii
assert token ≡ 'yes'
let value bad ← "not-a-number"
let int recovered ← bad ↦ int ⊥ 0
assert recovered ≡ 0
print count
print record
print widened
print flag
print greeting
print wire
print token
print recovered
}Expected output:
42
3.5
7.0
true
salve
1979-05-27T07:32:00Z
yes
0
radix/corpus/reference/conversio/valor/valor-tensor.fab (canonical · conversio)#
Compose value ↔ tensor roundtrip via list bridges (no dedicated value↔tensor arms).
# =============================================================================
# value — Compose value ↔ tensor roundtrip via list bridges (no dedicated value↔tensor arms).
# =============================================================================
#
# What this teaches:
# • Indirect tensor conversion — `value ↔ tensor` has no dedicated codegen arms; it materializes through `list<T>` as an intermediate step.
# • Roundtrip path — value → list → tensor → list → value, showing composition of existing operators.
#
# Common mistakes:
# • Expecting `value ↦ tensor` to work directly without the list bridge — there are no dedicated value↔tensor arms; compose through `list<T>` instead.
#
# See also: ↦, value, list, tensor
# =============================================================================
# conversion — value ↔ tensor compose roundtrip
#
# WHY: `value ↔ tensor` has no dedicated codegen arms. Extraction materializes
# `list<T>` first; construction uses shipped `list ↦ tensor` (`structa`) and
# flatten via `tensor ↦ list` (`.flatten()`), then `list ↦ value` boxing.
main {
let value arr ← [1, 2, 3, 4]
let list<int> xs ← arr ↦ list<int>
assert xs.length() ≡ 4
let tensor<int, [4]> grid ← xs ↦ tensor<int, [4]>
let list<int> flat ← grid ↦ list<int>
assert flat.length() ≡ 4
let value roundtrip ← flat ↦ value
print roundtrip
let value obj ← { "alpha": 10, "beta": 20 } ↦ value
let map<string, int> scores ← obj ↦ map<string, int>
print scores.get("alpha") coalesce 0
}Expected output:
[1, 2, 3, 4]
10
radix/corpus/reference/existing-home/valor/literal.fab (canonical · existing-home)#
JSON-shaped dynamic value literal for open object-shaped data.
# =============================================================================
# value — JSON-shaped dynamic value literal for open object-shaped data.
# =============================================================================
#
# What this teaches:
# • JSON value literals — `{ "key": value, ... }` creates dynamic value objects using JSON5 syntax with trailing commas.
# • Nested objects — JSON value literals can be nested for complex data shapes.
# • Constraint — JSON value literals accept only constant values (strings, numbers, booleans, null, nested objects/arrays); use class or map for variable-backed construction.
#
# Common mistakes:
# • Using retired bare object literals `{ key = expr }` instead of class construction `Type { field = expr }` — bare `{ }` is now a JSON value literal with quoted keys and `:` separator.
#
# See also: class, ∷, unknown, map
# =============================================================================
# destructura — value literal expressions (inline JSON)
#
# { "clavis": expr, ... } -- JSON object literal (value)
# { "clavis": expr, "nidum": { ... } } -- nested JSON objects
#
# Anonymous Faber object literals (`{ key = expr }`) are retired. Bare `{ }`
# now denotes a JSON value 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 value, point coords, keyed fields, nested records.
#
# BACKEND: Go emitter does not lower object `spread` into maps (whitelist:
# destructura/literal.fab).
main {
# --- Empty and simple records ---
let json void ← {}
print void
let json punctum ← { "x": 10, "y": 20 }
print punctum
# Text keys and constant values
let json forma ← { "clavis": 42 }
print forma
# NOTE: JSON value literals accept only constant values (strings, numbers,
# booleans, null, nested objects/arrays). For variable-backed field
# construction, use class `Type { field = expr }` or map insertion.
# --- Nested value ---
let json nidum ← { "extra": { "medium": 1 } }
print nidum
}Expected output:
{}
{"x": 10, "y": 20}
{"clavis": 42}
{"extra": {"medium": 1}}