-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmethods.ko
More file actions
52 lines (42 loc) · 1.09 KB
/
methods.ko
File metadata and controls
52 lines (42 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Inherent impl blocks — methods on structs without requiring a trait.
// Phase 38a feature.
module methods {
meta {
version: "1.0.0"
purpose: "Demonstrates inherent impl blocks for struct methods"
author: "Kodo Team"
}
struct Point {
x: Int,
y: Int,
}
impl Point {
fn translate(self, dx: Int, dy: Int) -> Point {
return Point { x: self.x + dx, y: self.y + dy }
}
fn sum(self) -> Int {
return self.x + self.y
}
}
struct Counter {
value: Int,
}
impl Counter {
fn increment(self) -> Counter {
return Counter { value: self.value + 1 }
}
fn get(self) -> Int {
return self.value
}
}
fn main() -> Int {
let p: Point = Point { x: 3, y: 4 }
let moved: Point = p.translate(1, 2)
print_int(moved.sum())
let c: Counter = Counter { value: 0 }
let c2: Counter = c.increment()
let c3: Counter = c2.increment()
print_int(c3.get())
return 0
}
}