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
|
// Test cyclic detector when using iface instances.
enum Tree = TreeR;
type TreeR = @{
mut left: option<Tree>,
mut right: option<Tree>,
val: to_str
};
iface to_str {
fn to_str() -> str;
}
impl <T: to_str> of to_str for option<T> {
fn to_str() -> str {
alt self {
none { "none" }
some(t) { "some(" + t.to_str() + ")" }
}
}
}
impl of to_str for int {
fn to_str() -> str { int::str(self) }
}
impl of to_str for Tree {
fn to_str() -> str {
#fmt["[%s, %s, %s]",
self.val.to_str(),
self.left.to_str(),
self.right.to_str()]
}
}
fn foo<T: to_str>(x: T) -> str { x.to_str() }
fn main() {
let t1 = Tree(@{mut left: none,
mut right: none,
val: 1 as to_str });
let t2 = Tree(@{mut left: some(t1),
mut right: some(t1),
val: 2 as to_str });
let expected = "[2, some([1, none, none]), some([1, none, none])]";
assert t2.to_str() == expected;
assert foo(t2 as to_str) == expected;
t1.left = some(t2); // create cycle
}
|