summary refs log tree commit diff
path: root/src/test/run-pass/iface-generic.rs
blob: 21c195377c980a0aad70fc62521a74756465717d (plain)
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
iface to_str {
    fn to_str() -> str;
}
impl of to_str for int {
    fn to_str() -> str { int::str(self) }
}
impl of to_str for str {
    fn to_str() -> str { self }
}
impl of to_str for () {
    fn to_str() -> str { "()" }
}

iface map<T> {
    fn map<U>(f: fn(T) -> U) -> [U];
}
impl <T> of map<T> for [T] {
    fn map<U>(f: fn(T) -> U) -> [U] {
        let mut r = [];
        for x in self { r += [f(x)]; }
        r
    }
}

fn foo<U, T: map<U>>(x: T) -> [str] {
    x.map({|_e| "hi" })
}
fn bar<U: to_str, T: map<U>>(x: T) -> [str] {
    x.map({|_e| _e.to_str() })
}

fn main() {
    assert foo([1]) == ["hi"];
    assert bar::<int, [int]>([4, 5]) == ["4", "5"];
    assert bar::<str, [str]>(["x", "y"]) == ["x", "y"];
    assert bar::<(), [()]>([()]) == ["()"];
}