-
Notifications
You must be signed in to change notification settings - Fork 7
/
index_operator.oc
61 lines (51 loc) · 1004 Bytes
/
index_operator.oc
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
53
54
55
56
57
58
59
60
61
/// exit: 0
import std::vector::{ Vector }
struct Foo {
x, y, z: u32
}
[operator "[]"]
def Foo::index(this, i: u32): u32 => match i {
0 => this.x,
1 => this.y,
2 => this.z,
else => {
assert false, "index out of bounds for Foo::index"
}
}
[operator "[]="]
def Foo::set_index(&this, i: u32, v: u32) {
match i {
0 => this.x = v,
1 => this.y = v,
2 => this.z = v,
else => {
assert false, "index out of bounds for Foo::index"
}
}
}
def main() {
let f = Foo(1, 2, 3)
assert f[0] == 1
assert f[1] == 2
assert f[2] == 3
f[0] = 4
f[1] = 5
f[2] = 6
assert f[0] == 4
assert f[1] == 5
assert f[2] == 6
let v = Vector<u32>::new()
v.push(1)
v.push(2)
v.push(3)
assert v[0] == 1
assert v[1] == 2
assert v[2] == 3
v[0] = 4
v[1] = 5
v[2] = 6
assert v[0] == 4
assert v[1] == 5
assert v[2] == 6
println("All tests passed!")
}