shared
Translation status: English reader-locale proof. Term names and code fences follow the en pack; supporting prose may still be English.
Declares shared fields once on a tagged union.
Aliases: shared
Syntax: @ shared
Category#
type
Related#
Examples#
radix/corpus/commune/commune.fab (canonical · annotation)#
Declares shared fields once on a tagged union.
# =============================================================================
# shared — Declares shared fields once on a tagged union.
# =============================================================================
#
# What this teaches:
# • Shared fields — `@ shared` (English `@ shared`) declares fields every
# variant carries. Construction supplies them next to the variant payload.
# • Member access — `e.message` reads the shared field on the union value.
# Matching still binds the same field in case arms.
#
# Common mistakes:
# • Redeclaring a shared field on a variant — the field is already on the
# union; repeating it on a variant is rejected.
# • Annotating `@ shared` on a class field — the region is union-only.
#
# See also: union, match, variant
# =============================================================================
#
# BACKEND:
# TEU3: shared fields emit on the primary lanes (Rust enum payloads, TS
# tagged unions) and MIR lowering/stepper. Tail backends are TEU4.
union ShapeError {
@ shared
string message
Incompatible,
ElementMismatch {
int expected
int actual
}
}
fn describe(ShapeError e) → string {
match e {
case Incompatible const message { return message }
case ElementMismatch const message, expected, actual { return message }
}
}
fn render(ShapeError e) → string {
return e.message
}
main {
const ShapeError a ← variant Incompatible { message = "incompatible" }
const ShapeError b ← variant ElementMismatch { message = "mismatch", expected = 1, actual = 2 }
print describe(a)
print describe(b)
print render(a)
}