Skip to content

Impl Methods

Use impl to attach methods to a struct.

struct User {
name: string
}
impl User {
fn greet(self: User) string {
return "Hello, " + self.name
}
}

Create an instance and call the method.

let user = User()
user.name = "Martin"
println(user.greet())

When a function attached to a struct expects the struct instance as its first argument, Nubo binds the current instance automatically.

This method:

fn greet(self: User) string {
return "Hello, " + self.name
}

Can be called like this:

user.greet()

Nubo passes user as the first argument internally.

struct User {
name: string
}
impl User {
fn rename(self: User, name: string) void {
self.name = name
}
}
let user = User()
user.rename("Nubo")
println(user.name)
struct Point {
x: int
y: int
}
impl Point {
fn label(self: Point) string {
return "(" + string(self.x) + ", " + string(self.y) + ")"
}
}
let point = Point()
point.x = 10
point.y = 20
println(point.label())
struct Counter {
value: int
}
impl Counter {
fn increment(self: Counter) void {
self.value = self.value + 1
}
}
let counter = Counter()
counter.increment()
counter.increment()
println(counter.value)

Methods added inside impl are stored on the struct prototype.

Each instance receives those implemented methods.

let a = Counter()
let b = Counter()
a.increment()
b.increment()
b.increment()
println(a.value)
println(b.value)

Each instance has its own fields, but they share the same implemented behavior.