LEARN VOID / 07

Structs & memory

Model the things you work with.

Value copies, shared objects and automatic memory management.

structs.voidDownload source ↓
value struct point:
    x: i32
    y: i32

struct counter:
    total: i32

func entry(value: i32) -> i32:
    var original = point(x = 2, y = 3)
    var copied = original
    copied.x += 10
    var shared = counter(total = 0)
    var alias = shared
    alias.total += 5
    return original.x + copied.x + shared.total
Input 0Expected result 19

Copy a value

value struct point groups two fields into a value. Assigning original to copied gives the copy independent value storage. Changing copied.x leaves original.x at 2.

Share an object

An ordinary struct is a managed reference object. Here, shared and alias refer to the same counter, so changing alias.total also changes the total observed through shared.

Let memory management stay automatic

The tracing garbage collector reclaims unreachable managed objects. Ordinary application code does not manually free them. Resource cleanup is a separate concern: garbage collection is not a substitute for deterministic release of external resources.

In this example, the result is 2 + 12 + 5 = 19. A value struct containing managed references still preserves the identity of those referenced objects when copied.