impl Blocks and Instance Methods
An impl block groups instance methods for a type. Each method receives
self as its first parameter — the instance on which the method was called.
Methods inside the same impl can call each other via self.method():
current() reads the field; doubled() calls current() to compose the result.
// Feature: impl block — groups methods of a struct
// Syntax: `impl Type { fn method(self, ...) { ... } }`
// When to use: attach behavior to a type. Each impl block adds
// instance methods (which take `self`) or associated functions
// (no `self`, called via `Type::name(...)`).
struct Counter {
value: int,
}
impl Counter {
// Instance method: takes `self` as the first parameter.
fn current(self) -> int {
return self.value
}
// Another instance method — can call other methods on self.
fn doubled(self) -> int {
return self.current() * 2
}
}
let c = Counter { value: 7 }
print(c.current()) // 7
print(c.doubled()) // 14
// expected:
// 7
// 14
Methods that return new values follow the immutable style: they compute and return a new instance without modifying the original. This makes reasoning about state easier:
scaled(factor) returns a new Rectangle — the instance r is unchanged.
// Feature: Instance methods — they use `self`
// Syntax: `fn method(self, args...) -> Ret { ... }`
// When to use: read/use instance data, or return a new modified
// instance (immutable style).
struct Rectangle {
width: float,
height: float,
}
impl Rectangle {
fn area(self) -> float {
return self.width * self.height
}
fn perimeter(self) -> float {
return 2.0 * (self.width + self.height)
}
fn is_square(self) -> bool {
return self.width == self.height
}
// Returns a NEW instance — functional immutable pattern.
fn scaled(self, factor: float) -> Rectangle {
return Rectangle { width: self.width * factor, height: self.height * factor }
}
}
let r = Rectangle { width: 4.0, height: 5.0 }
print(r.area()) // 20
print(r.perimeter()) // 18
print(r.is_square()) // false
let big = r.scaled(2.0)
print(big.area()) // 80
// expected:
// 20
// 18
// false
// 80
When behavior needs to change state, assign directly to the field via
self.field = new_value. The method can then return the resulting value or
self itself:
increment() and add(n) mutate self.value and return the new value.
// Feature: `self` in methods — reading and mutating fields
// Syntax: `fn method(self, ...) { self.field = new_value }`
// When to use: read or directly modify the fields of the instance
// that received the call.
struct Counter {
value: int,
}
impl Counter {
fn new() -> Counter {
return Counter { value: 0 }
}
// Direct mutation via self — updates the instance field.
fn increment(self) -> int {
self.value = self.value + 1
return self.value
}
// Simple read.
fn get(self) -> int {
return self.value
}
fn add(self, n: int) -> int {
self.value = self.value + n
return self.value
}
}
let c = Counter::new()
print(c.increment()) // 1
print(c.increment()) // 2
print(c.add(10)) // 12
print(c.get()) // 12
// expected:
// 1
// 2
// 12
// 12
Challenge
In 02-instance-methods.zolo, add a method with_height(h) that returns a
new Rectangle with the original width but height h. Call it chained:
r.scaled(2.0).with_height(1.0).area().