Skip to content

Commit 93d425e

Browse files
committed
Simple example of anonymous objects from nothing. Closes #812.
1 parent 886c2ee commit 93d425e

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
use std;
2+
3+
fn main() {
4+
5+
// Anonymous object that doesn't extend an existing one.
6+
let my_obj = obj() {
7+
fn foo() -> int { ret 2; }
8+
fn bar() -> int { ret 3; }
9+
fn baz() -> str { "hello!" }
10+
};
11+
12+
assert my_obj.foo() == 2;
13+
assert my_obj.bar() == 3;
14+
assert my_obj.baz() == "hello!";
15+
16+
// Make sure the result is extendable.
17+
let my_ext_obj = obj() {
18+
fn foo() -> int { ret 3; }
19+
fn quux() -> str { ret self.baz(); }
20+
with my_obj
21+
};
22+
23+
assert my_ext_obj.foo() == 3;
24+
assert my_ext_obj.bar() == 3;
25+
assert my_ext_obj.baz() == "hello!";
26+
assert my_ext_obj.quux() == "hello!";
27+
28+
// And again.
29+
let my_ext_ext_obj = obj() {
30+
fn baz() -> str { "world!" }
31+
with my_ext_obj
32+
};
33+
34+
assert my_ext_ext_obj.foo() == 3;
35+
assert my_ext_ext_obj.bar() == 3;
36+
assert my_ext_ext_obj.baz() == "world!";
37+
assert my_ext_ext_obj.quux() == "world!";
38+
}

0 commit comments

Comments
 (0)