Tagless overlapping storage for one value chosen from a fixed list of schemas.


Union

Construct with a schema list in tuple form, for example (Int32 Nat8) Union.

Union

(alternatives -- union) Constructs untagged overlapping storage.
  • Construction initializes the raw storage fields but does not initialize or destroy an alternative's managed value.
  • Use a nonempty List or Tuple of alternative-schema objects, such as (Int32 Nat8). Their schemas and views describe the alternatives; their stored prototype values are not copied into the payload.
  • Union requires compatible iteration over the ordinal alternatives.
  • A plain named Dict without iteration fails with Built-in text, tuple, Iter, or Iterable expected.

Remarks

  • Alternatives need supported storage size and alignment. Direct Text and Code alternatives are not supported. An accessed alternative must not be Meta.

Fields

Storage rules

Methods

get

(key -- ref) Returns a borrowed typed view of the overlapping storage interpreted as the schema at key.
  • key is an Int32 ordinal. A known key selects an in-range alternative; unknown keys require uniform alternative schemas. Runtime bounds are not checked for an unknown key, so the caller must keep it in range.
  • No active tag or stored-representation check is performed. Access through another valid key reinterprets the same storage.
  • The result is mutable only when both the receiver view and the selected prototype view permit mutation; otherwise it is immutable. Use mutable prototype views when writable alternatives are required.

MAXIMUM_ALIGNMENT

(-- alignment) Returns the greatest alternative alignment in bytes as Int32, with a minimum of 1.

Remarks

  • This is a compile-time layout query over the stored alternative-schema descriptor, not a per-instance counter.

MAXIMUM_SIZE

(-- size) Returns the greatest alternative storage size in bytes as Int32, with a minimum of 1.

Remarks

  • This is a compile-time layout query over the stored alternative-schema descriptor, not a per-instance counter.

Getter result schemas

"Union"   use
"control" use

{} () {} [
  u: (Int32 Nat8) Union;
  0 @u.get printStack _:;
  1 @u.get printStack _:;
] "main" exportFunction

Expected Output During Compilation

Int32 Ref
Nat8 Ref

Write through typed refs

"String"  use
"Union"   use
"control" use

{} Int32 {} [
  u: (Int32 Nat8) Union;
  123 0 @u.get set
  ("int=" 0 @u.get new LF) printList
  7n8 1 @u.get set
  ("nat=" 1 @u.get new LF) printList
  0
] "main" exportFunction

Expected Output

int=123
nat=7

Reinterpret the same storage through another key

"String"  use
"Union"   use
"control" use

{} Int32 {} [
  u: (Int32 Nat32) Union;
  -1 0 @u.get set
  ("asInt=" 0 @u.get new LF
    "asNat=" 1 @u.get new LF) printList
  0
] "main" exportFunction

Expected Output

asInt=-1
asNat=4294967295

Storage, lifetime, and copy semantics


See also