LEARN VOID / DATA & BEHAVIOR
Receiver methods
Keep behavior close to the data it uses.
Complete program for the synchronous entry adapter. The result below is the returned integer.
value struct point:
x: i32
y: i32
func (position point) move(dx: i32 = 0, dy: i32 = 0):
position.x += dx
position.y += dy
func (position point) distance_squared() -> i32:
return position.x * position.x + position.y * position.y
func entry(steps: i32) -> i32:
var cursor = point(x = 0, y = 0)
cursor.move(dy = steps, dx = steps)
const final_position = cursor
return final_position.distance_squared()Name the receiver
The receiver position identifies the point used by each method. move accepts named arguments and defaults each offset to zero. The call supplies dy before dx, making their meaning visible. It updates coordinates through that named receiver; distance_squared only reads them.
Move before measuring
The cursor starts at the origin and moves equally along both axes. Input 5 produces position (5, 5) and squared distance 25 + 25 = 50. This is squared distance, not a square-root calculation.
Read through a readonly value
const final_position = cursor makes a readonly value copy. The non-mutating measurement method is allowed through that binding. A call to final_position.move would be rejected. Ordinary assignment/passing of a value struct still copies its storage.