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


Lifetime and mutability


Fields


Methods

CALL

(-- ref) Reconstructs the current referenced object from the stored address.

Postconditions

  • The result schema is supplied by getSchema and reconstructed through addressToReference.

assign

(source --) Stores the address of the supplied target in the wrapper.

Preconditions

  • The dereferenced schemas of source and the supplier's representative must match. For a writable representative, an immutable source Ref is rejected; pass a mutable view such as @value. An In-place source is also accepted. An immutable representative accepts either source view.
  • assign is called through a mutable wrapper view, as in @holder.assign.
  • The target remains valid for later CALL reconstruction.
  • source is not the unchanged result of Ref or Cref. Assertion checking reports Reference is undefined at run time; the check is omitted when the selected DEBUG definition is off, for example with -ndebug.

equal

(other -- cond) Compares the stored address with that of other.

get

(-- ref) Returns what CALL returns.

getHash

(-- hash) Returns a Nat32 hash of the stored address.

to

(source -- mref) Creates a wrapper with the same getSchema and TARGET_SCHEMA_NAME, passes source to its assign, and returns the wrapper.

Preconditions

  • The preconditions of assign apply to source.

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