Version: 1.1.0
Self-referential structs
A struct cannot contain a field of its own type directly, because the compiler needs a finite size. Pointers are how you build lists, trees, and graphs.
Essentials
- Store
UnsafePointer[Node]or an index, notNodeitself. - Allocate each node with the allocator you intend to free.
- Destruction must walk the structure once so nothing is freed twice.
- Indexes into one array are often safer than a web of raw pointers.
Example
struct Node[T: Movable]:
var value: T
var next: UnsafePointer[Self]
Keep in mind
Prefer an existing collection until a custom linked structure is actually required. Pointer lifetime is your responsibility.