-
Notifications
You must be signed in to change notification settings - Fork 13k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simple example of anonymous objects from nothing. Closes #812.
- Loading branch information
Showing
1 changed file
with
38 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!"; | ||
} |