rust/src/test/run-pass/anon-obj-overriding.rs

39 lines
747 B
Rust
Raw Normal View History

use std;
fn main() {
obj a() {
2011-07-27 07:19:39 -05:00
fn foo() -> int { ret 2; }
fn bar() -> int { ret self.foo(); }
}
2011-07-27 07:19:39 -05:00
let my_a = a();
// An anonymous object that overloads the 'foo' method.
2011-07-27 07:48:34 -05:00
let my_b =
2011-07-27 07:19:39 -05:00
obj () {
fn foo() -> int { ret 3; }
with
my_a
};
assert (my_b.foo() == 3);
assert (my_b.bar() == 3);
2011-07-20 17:46:56 -05:00
2011-07-27 07:19:39 -05:00
let my_c =
obj () {
fn baz(x: int, y: int) -> int { ret x + y + self.foo(); }
with
my_b
};
2011-07-20 17:46:56 -05:00
2011-07-27 07:19:39 -05:00
let my_d =
obj () {
fn baz(x: int, y: int) -> int { ret x + y + self.foo(); }
with
my_a
};
2011-07-20 17:46:56 -05:00
assert (my_c.baz(1, 2) == 6);
assert (my_d.baz(1, 2) == 5);
2011-07-27 07:19:39 -05:00
}