Heap-allocated reference type. Similar to POINTER TO but with managed semantics. Requires --m2plus.
TYPE T = REF MyRecord;
REF Tcreates a reference to a heap-allocated value of type T.- Values are created with
NEW(r)which allocates viaM2_ref_alloc. This prepends a hiddenM2_RefHeadercontaining a type descriptor pointer before the payload, then returns a pointer to the payload. - Dereference with
r^to access the underlying value. - Each REF type gets a unique
M2_TypeDescwith a type ID, name, parent pointer, and depth. This enables TYPECASE dispatch viaM2_ISA(parent-chain walk with depth early-out). - Optional Boehm GC support: compile with
-DM2_USE_GCto use garbage collection instead of manual deallocation. Falls back tomalloc/freeautomatically ifgc/gc.his not installed. - REF is distinct from
POINTER TO-- REF values carry a type descriptor header and are compatible with REFANY; plain pointers are not.
TYPE
IntRef = REF RECORD value: INTEGER END;
VAR
r: IntRef;
BEGIN
NEW(r);
r^.value := 42;
WriteInt(r^.value, 0); WriteLn;
END RefDemo.