LEARN VOID / DATA & BEHAVIOR

Composition

Build a type from smaller pieces of behavior.

Complete program for the synchronous entry adapter. The result below is the returned integer.

composition.voidDownload source
value struct position:
    x: i32

func (location position) advance(steps: i32):
    location.x += steps

struct cursor:
    using location: position

func entry(steps: i32) -> i32:
    var pointer = cursor(location = position(x = 10))
    pointer.advance(steps)
    return pointer.x
Input 5Expected result 15

Build from a component

The cursor stores a position as location. using location: position promotes accessible members, so the program can call pointer.advance and read pointer.x without manually forwarding them.

Follow the contained value

The component starts at ten and advances by the input. With 5 steps, both pointer.x and pointer.location.x refer to the updated coordinate 15. The promoted call still operates on the cursor’s contained position.

Keep the component’s semantics

Composition does not change ownership. Copying a value component copies its value storage; a managed component retains shared reference identity. The ordinary cursor itself is managed. Promotion also preserves visibility, receiver-effect and readonly checks.