LEARN VOID / DATA & BEHAVIOR

Receiver methods

Keep behavior close to the data it uses.

methods.voidDownload source ↓
value struct point:
    x: i32 = 2
    y: i32 = 3

func (p point) move(dx: i32, dy: i32):
    p.x += dx
    p.y += dy

func (p point) distance_squared() -> i32:
    return p.x * p.x + p.y * p.y

func entry(value: i32) -> i32:
    var position = point()
    position.move(value, 0)
    return position.distance_squared()
Input 5Expected result 58

Name the receiver

func (p point) move declares a method for point. The receiver has an explicit name; the method reads and changes fields through p.

Update a mutable value

Calling position.move updates the mutable local value. Passing or assigning the value ordinarily still copies it. With input 5, position becomes (7, 3), and distance_squared returns 58.

Respect readonly access

A readonly value can call methods proven non-mutating, such as distance_squared. Mutating methods remain subject to receiver and visibility checks.