Skip to content

Commit c2382f9

Browse files
committed
Add test for issue 2287
Test that it's possible to write "impl of B for A" outside a class, where A is a class type. This didn't require any extra work, but it's still good to have a regression test. Closes #2287
1 parent f32d9f4 commit c2382f9

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import to_str::*;
2+
import to_str::to_str;
3+
4+
class cat {
5+
priv {
6+
let mut meows : uint;
7+
fn meow() {
8+
#error("Meow");
9+
self.meows += 1u;
10+
if self.meows % 5u == 0u {
11+
self.how_hungry += 1;
12+
}
13+
}
14+
}
15+
16+
let mut how_hungry : int;
17+
let name : str;
18+
19+
new(in_x : uint, in_y : int, in_name: str)
20+
{ self.meows = in_x; self.how_hungry = in_y; self.name = in_name; }
21+
22+
fn speak() { self.meow(); }
23+
24+
fn eat() -> bool {
25+
if self.how_hungry > 0 {
26+
#error("OM NOM NOM");
27+
self.how_hungry -= 2;
28+
ret true;
29+
}
30+
else {
31+
#error("Not hungry!");
32+
ret false;
33+
}
34+
}
35+
}
36+
37+
impl of to_str for cat {
38+
fn to_str() -> str { self.name }
39+
}
40+
41+
fn print_out<T: to_str>(thing: T, expected: str) {
42+
let actual = thing.to_str();
43+
#debug("%s", actual);
44+
assert(actual == expected);
45+
}
46+
47+
fn main() {
48+
let nyan : to_str = cat(0u, 2, "nyan") as to_str;
49+
print_out(nyan, "nyan");
50+
}

0 commit comments

Comments
 (0)