summary refs log tree commit diff
path: root/src/test/run-pass/bind-methods.rs
blob: 2eb20ed0e9d2c8c9c883ad212d1d2a0d9dbbec1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
iface foo {
    fn foo() -> int;
    fn bar(p: int) -> int;
}
impl of foo for int {
    fn foo() -> int { self }
    fn bar(p: int) -> int { p * self.foo() }
}
impl <T: foo> of foo for [T] {
    fn foo() -> int { vec::foldl(0, self, {|a, b| a + b.foo()}) }
    fn bar(p: int) -> int { p + self.len() as int }
}

fn main() {
    let x = [1, 2, 3];
    let y = x.foo, z = [4, 5, 6].foo;
    assert y() + z() == 21;
    let a = x.bar, b = bind [4, 5, 6].bar(_);
    assert a(1) + b(2) + z() == 24;
}