rust/src/test/run-pass/simple-anon-objs.rs

30 lines
528 B
Rust
Raw Normal View History

2011-05-20 17:41:36 -07:00
use std;
fn main() {
2011-06-16 11:40:09 -07:00
obj normal() {
fn foo() -> int { ret 2; }
2011-05-20 17:41:36 -07:00
}
2011-06-16 11:40:09 -07:00
auto my_normal_obj = normal();
2011-05-20 17:41:36 -07:00
2011-06-16 11:40:09 -07:00
// Extending an object with a new method
auto my_anon_obj = obj {
fn bar() -> int {
ret 3;
}
2011-06-16 11:40:09 -07:00
with my_normal_obj
};
2011-05-20 17:41:36 -07:00
2011-06-16 11:40:09 -07:00
assert (my_normal_obj.foo() == 2);
assert (my_anon_obj.bar() == 3);
2011-06-16 11:40:09 -07:00
auto another_anon_obj = obj {
fn baz() -> int {
ret 4;
}
2011-06-16 11:40:09 -07:00
with my_anon_obj
};
2011-06-16 11:40:09 -07:00
assert (another_anon_obj.baz() == 4);
}