Skip to content

Commit

Permalink
Simple example of anonymous objects from nothing. Closes #812.
Browse files Browse the repository at this point in the history
  • Loading branch information
lkuper committed Aug 15, 2011
1 parent 886c2ee commit 93d425e
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/test/run-pass/anon-obj-no-inner-obj-simple.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use std;

fn main() {

// Anonymous object that doesn't extend an existing one.
let my_obj = obj() {
fn foo() -> int { ret 2; }
fn bar() -> int { ret 3; }
fn baz() -> str { "hello!" }
};

assert my_obj.foo() == 2;
assert my_obj.bar() == 3;
assert my_obj.baz() == "hello!";

// Make sure the result is extendable.
let my_ext_obj = obj() {
fn foo() -> int { ret 3; }
fn quux() -> str { ret self.baz(); }
with my_obj
};

assert my_ext_obj.foo() == 3;
assert my_ext_obj.bar() == 3;
assert my_ext_obj.baz() == "hello!";
assert my_ext_obj.quux() == "hello!";

// And again.
let my_ext_ext_obj = obj() {
fn baz() -> str { "world!" }
with my_ext_obj
};

assert my_ext_ext_obj.foo() == 3;
assert my_ext_ext_obj.bar() == 3;
assert my_ext_ext_obj.baz() == "world!";
assert my_ext_ext_obj.quux() == "world!";
}

0 comments on commit 93d425e

Please sign in to comment.