LEARN VOID / 07
Structs & memory
Choose value copies or shared objects for your data.
Complete program for the synchronous entry adapter. The result below is the returned integer.
value struct position:
x: i32
func entry(start: i32) -> i32:
var original = position(x = start)
var moved = original
moved.x += 3
return original.xCopy a value independently
The first program copies a position value, then moves only the copy by three. Input 5 leaves original.x at 5 while moved.x becomes 8. Returning the original makes the independence visible.
Share a managed object
The second program uses an ordinary managed struct. Both basket names refer to one object. Adding three items through shared_basket is visible through basket, so input 5 returns 8.
struct shopping_basket:
item_count: i32
func entry(items: i32) -> i32:
var basket = shopping_basket(item_count = items)
var shared_basket = basket
shared_basket.item_count += 3
return basket.item_countSeparate memory from resources
The tracing garbage collector reclaims unreachable managed objects without manual frees. A value struct containing managed references still shares the referenced objects when copied. Readonly views preserve their access restrictions. External-resource release is separate from garbage collection; use deterministic cleanup for scoped ownership.