Tagged multi-schema storage with checked access and reassignment.


Variant

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

Variant

(alternatives -- variant) Constructs an owning tagged-storage object, selects tag 0, and initializes that alternative according to its lifetime rules.
  • 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.
  • Named Struct fields are also accepted and are indexed by ordinal.
  • The object owns its active alternative. INIT selects tag 0 and initializes that alternative; normal destruction runs the destruction operation for the alternative selected by its valid tag.

Remarks

  • A managed prototype with an INIT operation initializes according to that operation rather than copying its stored value. An INIT field without a managed lifetime does not change initialization.
  • Alternatives need supported storage size and alignment. Direct Text and Code alternatives are not supported. An accessed alternative must not be Meta.

Fields

Layout

Methods

rawInit

(--) Initializes the alternative selected by the current valid tag without changing that tag.

Remarks

  • Use only when that alternative's storage is not already live; initializing over a live owned value can lose its resources.

rawDestroy

(--) Destroys the alternative selected by the current valid tag without changing that tag.

Remarks

  • Do not destroy the alternative twice; restore a valid initialized state before normal destruction of the Variant.

getTag

(-- tag) Returns the current zero-based alternative ordinal as an Int32 object.
  • The result is an object, not a reference.

setTag

(key --) Changes the active alternative when key differs from the stored tag.
  • key must be an Int32. It accepts a known or unknown key and does not check the range; an unsupported key is stored without initializing an alternative.
  • When key equals the current tag, the existing payload is unchanged. Otherwise the old alternative is destroyed, key is stored, and the new alternative is initialized.

getUnchecked

(key -- ref) Returns a borrowed typed view of storage for key without checking the stored tag.
  • key must be an Int32 ordinal.
  • No tag check is performed; the caller must ensure that the requested alternative is valid to access. An unknown key requires uniform alternative schemas. Unknown-key bounds are the caller's responsibility. Use visit to dispatch typed access to heterogeneous alternatives when the tag is unknown.

get

(key -- ref) Returns the selected storage view after checking that key equals the stored tag.
  • A known mismatch is a compile-time error, Wrong tag in Tagged Union!, in either DEBUG mode.
  • An unknown mismatch reports Wrong tag in Tagged Union! and exits with status 2 when DEBUG is enabled. The runtime tag check is omitted when the selected DEBUG definition is off, for example with -ndebug.
  • The caller must still ensure that the requested alternative is valid to access.
  • The same Int32 ordinal and knownness requirements as getUnchecked apply.

assign

(value key --) Selects key and assigns value to that alternative.
  • If the selected alternative has a field named assign, value is left on the stack for the member access .assign, which assigns nothing when the field is not a method. Otherwise value must have the alternative's schema; assignment requires the relevant copy/move support and follows the input view's rules: a mutable managed input can be moved from, while an immutable input is copied.
  • A tag change destroys the old alternative before initializing and assigning the new one.

match

(... branches -- ...) Dispatches the branch of the first guard that returns TRUE for the active payload, or an optional fallback.
  • branches is one Struct containing alternating guards and call-supported branch objects, optionally followed by one fallback. A guard is a call-supported object with effect (... payload -- ... cond).
  • Guards are tried in order; a guard whose call has an ordinary compilation failure is skipped with its branch. If no guard returns TRUE, the fallback is called; without a fallback no branch is called.
  • Each guard and the called branch, the fallback included, receive the payload view that get would return for the active alternative on top of the carried stack.
  • Guard and branch calls may consume the carried stack and return additional objects. A Block guard or branch has a call boundary.
  • Only paths not excluded by a known tag, a known guard result or a skipped guard are processed. These paths, even those that call no branch, must compile and produce compatible output counts and schemas, including reference schemas.

visit

(... branches -- ...) Dispatches the branch selected by the active tag.
  • branches is one Struct containing alternating known Int32 keys and call-supported branch objects, optionally followed by one fallback.
  • Every alternative needs exactly one branch: the one after its key or, for an alternative without a key, the fallback. A repeated key, a key that names no alternative, an alternative without a branch and a fallback that no alternative needs are compile-time errors.
  • The selected branch, the fallback included, receives the payload view that get would return for the active alternative on top of the carried stack.
  • Branch calls may consume the carried stack and return additional objects. A Block branch has a call boundary.
  • With a known tag only the selected branch is processed. An unknown tag requires each alternative's branch to compile and produce compatible output counts and schemas, including reference schemas.

equal

(other -- cond) Compares variants with matching alternative-list schemas and active payloads.
  • Prototype values do not matter. A schema mismatch is a compile-time error, Variants' supported types differ.
  • A non-iterable Dict without equal fails with Built-in text, tuple, Iter, or Iterable expected.
  • Different active tags return FALSE. Matching tags compare the active payloads using library = dispatch, including iterable and equal-method adapters; the builtin = covers Cond and numeric Variant payloads; direct Text alternatives are unsupported.
  • Payload equality must be supported for every alternative whose comparison is processed. Unknown tags can require comparison support for alternatives that are not active at run time.

Example: assign, switch tag, and compare

A new variant has tag 0; assign sets the tag with the value, and equal is TRUE only while both variants hold the same tag and value.

"String"  use
"Variant" use
"control" use

{} Int32 {} [
  v0: (Int32 Nat8) Variant;
  v1: (Int32 Nat8) Variant;
  ("tag0=" v0.getTag LF) printList
  7n8 1 @v0.assign
  7n8 1 @v1.assign
  ("tag1=" v0.getTag LF
    "value1=" 1 @v0.get LF
    "equal0=" @v1 @v0.equal LF) printList
  123 0 @v1.assign
  ("tag2=" v1.getTag LF
    "value2=" 0 @v1.get LF
    "equal1=" @v1 @v0.equal LF) printList
  0
] "main" exportFunction

Expected Output

tag0=0
tag1=1
value1=7
equal0=TRUE
tag2=0
value2=123
equal1=FALSE

Example: visit branch dispatch

visit runs the branch for the current tag, 1, with the stored value 7.

"String"  use
"Variant" use
"control" use

{} Int32 {} [
  v: (Int32 Nat8) Variant;
  7n8 1 @v.assign
  (0 [x:; ("int=" x LF) printList]
    1 [x:; ("nat=" x LF) printList]) @v.visit
  0
] "main" exportFunction

Expected Output

nat=7

Lifetime and whole-object transfer


Access and reference stability


Module helper

getHeapUsedSize

(variant -- size) Returns the active payload's heap usage as Natx.

Known issue: the result is 0nx unless measuring the payload uses a getHeapUsedSize marked with overload; the Standard Library has none.

See also