Address-based non-owning wrapper that reconstructs one referenced object through one schema-supplier helper. It exists because a schema cannot contain a Ref to itself: write next: [Node] Mref; inside Node where next: Node Ref; would recurse without end; the Block is called only when the wrapper is used.
Construction forms
Mref has effect (getSchema -- mref). In the forms below, getSchema is a Block with effect (-- representative). The first form uses the schema and mutability of the representative returned by getSchema; the second applies Cref to make that representative immutable. An In-place or mutable Ref representative permits a writable result only through a mutable wrapper view.
myRef: @getSchema Mref; # writable if getSchema returns an In-place or mutable Ref representative
myRef: [getSchema Cref] Mref; # immutable representative and result
getSchema is one helper that returns one representative object or reference of the required schema.
Stored address model
datastores one address asNatx.- A new wrapper holds address
0nx; untilsetis called,CALLreturns a knownNILreference of the supplied schema, which cannot be dereferenced. CALLreconstructs the referenced object from that address throughgetSchemaandaddressToReference.setreplaces only the stored address. The wrapper does not copy or own the target object.
Lifetime and mutability
Mrefnever destroys or frees the stored target object.- Later
CALLrequires the target object to remain valid at the stored address. - The result is mutable only when both the supplier's representative is mutable (for example
@getSchema MrefwithgetSchemareturning an In-place0) and the wrapper is reached through a mutable view; an immutable representative ([getSchema Cref] Mref) or an immutable wrapper view (box.m,@holder const) gives an immutable result.
Methods
CALL
(-- ref) Reconstructs the current referenced object from the stored address.Postconditions
- The result schema is supplied by
getSchemaand reconstructed throughaddressToReference.
set
(source --) Stores the address of the supplied target in the wrapper.Preconditions
- The dereferenced schemas of
sourceand the supplier's representative must match. For a writable representative, an immutable sourceRefis rejected; pass a mutable view such as@value. An In-place source is also accepted. An immutable representative accepts either source view. setis called through a mutable wrapper view, as in@holder.set.- The target remains valid for later
CALLreconstruction.
Fields
SCHEMA_NAME: reported schema name,"Mref".getSchema: helper used to reconstruct the target schema.data: stored address asNatx.
Examples
Mutable and immutable wrappers
"Mref" use
"control" use
Int32Schema: [0];
{} () {} [
value: 5;
holderA: @Int32Schema Mref;
@value @holderA.set
holderA printStack _:;
imholderA: [Int32Schema Cref] Mref;
@value @imholderA.set
imholderA printStack _:;
] "main" exportFunction
Expected Output During Compilation
Int32 Ref
Int32 Cref
Runtime example
"Mref" use
"String" use
"control" use
Int32Schema: [0];
{} Int32 {} [
value0: 5;
value1: 9;
holder: @Int32Schema Mref;
@value0 @holder.set
("first=" holder new LF) printList
@value1 @holder.set
("second=" holder new LF) printList
0
] "main" exportFunction
Expected Output
first=5
second=9
See also
- Owner: Ownership management and move/transfer semantics.
addressToReferencebuiltin(address modelValue -- reconstructedValue)Builds a typed result from a numeric address and a model object that supplies schema and mutability.storageAddressbuiltin(object -- address)Returns the storage address of one non-Meta object asNatx.- Ref: Reference schema kind whose exact schema also includes the target schema and mutability.