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] "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 targetSchemaName -- mref). In the forms below, getSchema is a Block with effect (-- representative) and targetSchemaName is a known non-NIL Text. 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 "TargetSchemaName" Mref; # writable if getSchema returns an In-place or mutable Ref representative
myRef: [getSchema Cref] "TargetSchemaName" Mref; # immutable representative and result
getSchema is one helper that returns one representative object or reference of the required schema. targetSchemaName is not compared with that schema; it becomes part of the wrapper's schema name, so wrappers constructed with different names have different schemas.
Stored address model
- A new wrapper holds address
0nx; while the stored address is0nx,CALLreturns aNILreference of the supplied schema, which must not be dereferenced. CALLreconstructs the referenced object from the stored address throughgetSchemaandaddressToReference.assignreplaces 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 "TargetSchemaName" MrefwithgetSchemareturning an In-place0) and the wrapper is reached through a mutable view; an immutable representative ([getSchema Cref] "TargetSchemaName" Mref) or an immutable wrapper view (box.m,@holder const) gives an immutable result.
Fields
Methods
CALL
(-- ref) Reconstructs the current referenced object from the stored address.Postconditions
- The result schema is supplied by
getSchemaand reconstructed throughaddressToReference.
assign
(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. assignis called through a mutable wrapper view, as in@holder.assign.- The target remains valid for later
CALLreconstruction. sourceis not the unchanged result ofReforCref. Assertion checking reportsReference is undefinedat run time; the check is omitted when the selectedDEBUGdefinition is off, for example with-ndebug.
Examples
Example: mutable and immutable wrappers
@Int32Schema "Int32" Mref holds an Int32 Ref, and [Int32Schema Cref] "Int32" Mref holds an Int32 Cref.
"Mref" use
"control" use
Int32Schema: [0];
{} () {} [
value: 5;
holderA: @Int32Schema "Int32" Mref;
@value @holderA.assign
holderA printStack _:;
imholderA: [Int32Schema Cref] "Int32" Mref;
@value @imholderA.assign
imholderA printStack _:;
] "main" exportFunction
Expected Output During Compilation
Int32 Ref
Int32 Cref
Example: rebinding a wrapper
@holder.assign rebinds the wrapper from value0 to value1, so reading through it gives 5, then 9.
"Mref" use
"String" use
"control" use
Int32Schema: [0];
{} Int32 {} [
value0: 5;
value1: 9;
holder: @Int32Schema "Int32" Mref;
@value0 @holder.assign
("first=" holder new LF) printList
@value1 @holder.assign
("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.