Wu Language
Search…
Traits
Wu has straight up stolen trait behavior from Rust and Pony. Traits can be thought of as behavior criteria for a struct, defined as a set of methods one can expect to find on a struct.
A trait can be defined like so:
1
Movable: trait {
2
move: fun(self, dx: float, dy: foat)
3
}
Copied!
Thus, anything that implements Movablewill have to have a method named movethat takes the given parameters on it.
We can implement traits on a struct like this:
1
Vector: struct {
2
x: float
3
y: float
4
}
5
6
implement Vector: Movable {
7
move: fun(self, dx: float, dy: float) {
8
self x += dx
9
self y += dy
10
}
11
}
Copied!
Now, when we try to move a Vector using the movemethod it works perfectly:
1
point := new Vector {
2
x: 100
3
y: 100
4
}
5
6
point move(10, 5)
Copied!
Last modified 2yr ago
Copy link