Skip to content

Commit b02a77d

Browse files
committed
Add test for generic iface methods
Issue #1227
1 parent 94d40be commit b02a77d

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

src/test/run-pass/iface-generic.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
iface to_str {
2+
fn to_str() -> str;
3+
}
4+
impl of to_str for int {
5+
fn to_str() -> str { int::str(self) }
6+
}
7+
impl of to_str for str {
8+
fn to_str() -> str { self }
9+
}
10+
11+
iface map<T> {
12+
fn map<U>(f: block(T) -> U) -> [U];
13+
}
14+
impl <T> of map<T> for [T] {
15+
fn map<U>(f: block(T) -> U) -> [U] {
16+
let r = [];
17+
for x in self { r += [f(x)]; }
18+
r
19+
}
20+
}
21+
22+
fn foo<U, T: map<U>>(x: T) -> [str] {
23+
x.map({|_e| "hi" })
24+
}
25+
fn bar<U: to_str, T: map<U>>(x: T) -> [str] {
26+
x.map({|_e| _e.to_str() })
27+
}
28+
29+
fn main() {
30+
assert foo([1]) == ["hi"];
31+
assert bar::<int, [int]>([4, 5]) == ["4", "5"];
32+
assert bar::<str, [str]>(["x", "y"]) == ["x", "y"];
33+
}

0 commit comments

Comments
 (0)