Overview of Composites¶
A composite is a small anonymous record. It is useful when values belong
together for one operation but do not justify a named STRUCT.
AUTO result := :{
.value = 42 AS I32;
.was_cached = TRUE;
};
IF (result.was_cached)
{
use_cached_value(@value result.value);
}
Each field keeps its own type. A composite groups related values without a runtime dictionary or a requirement that every field have the same type.
Argument packs¶
Argument forwarding uses packs, whose fields are independent values. A composite provides ordinary struct storage for a related set of values, including records returned from functions and records stored in other objects.
Value and reference composites¶
A composite field can own a value or refer to an existing object:
VAR amount I32 := 12;
AUTO record := :{
.borrowed = amount;
.owned = amount AS I32;
};
record.borrowed := 20;
ASSERT(amount == 20);
ASSERT(record.owned == 12);
COMPOSITE_TIE and COMPOSITE_FORWARD create records whose fields refer to
existing objects. References avoid value copies. A reference field becomes
invalid when the source object's lifetime ends. Retaining the field without
using it is permitted. Any use of the invalidated reference is undefined
behavior, including accessing an object through it, creating another reference
from it, copying it, or taking its address. Value fields provide
independent storage and perform the corresponding copy or move.
Composites also support positional members, field selection, splitting, joining, compile-time reflection, and call argument expansion. The Composites Reference specifies those operations, field order, type identity, and lifetime rules.