-
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.
test: Simulate abstract methods with template modules
- Loading branch information
Showing
5 changed files
with
74 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,14 @@ | ||
type T = cat; | ||
|
||
enum cat { | ||
howlycat, | ||
meowlycat | ||
} | ||
|
||
fn animal() -> str { "cat" } | ||
fn talk(c: cat) -> str { | ||
alt c { | ||
howlycat { "howl" } | ||
meowlycat { "meow" } | ||
} | ||
} |
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,9 @@ | ||
type T = dog; | ||
|
||
enum dog { | ||
dog | ||
} | ||
|
||
fn animal() -> str { "dog" } | ||
fn talk(_d: dog) -> str { "woof" } | ||
|
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,9 @@ | ||
impl talky for T { | ||
|
||
// 'animal' and 'talk' functions are implemented by the module | ||
// instantiating the talky trait. They are 'abstract' | ||
fn says() -> str { | ||
animal() + " says '" + talk(self) + "'" | ||
} | ||
|
||
} |
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,28 @@ | ||
#[no_core]; | ||
|
||
|
||
#[path = "module-polymorphism4-files"] | ||
mod cat { | ||
|
||
import inst::*; | ||
|
||
#[path = "cat.rs"] | ||
mod inst; | ||
|
||
#[path = "trait.rs"] | ||
mod trait; | ||
|
||
} | ||
|
||
#[path = "module-polymorphism4-files"] | ||
mod dog { | ||
|
||
import inst::*; | ||
|
||
#[path = "dog.rs"] | ||
mod inst; | ||
|
||
#[path = "trait.rs"] | ||
mod trait; | ||
|
||
} |
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,14 @@ | ||
// This isn't really xfailed; it's used by the | ||
// module-polymorphism.rc test | ||
// xfail-test | ||
|
||
fn main() { | ||
import cat::trait::talky; | ||
import dog::trait::talky; | ||
let cat1 = cat::inst::meowlycat; | ||
let cat2 = cat::inst::howlycat; | ||
let dog = dog::inst::dog; | ||
assert cat1.says() == "cat says 'meow'"; | ||
assert cat2.says() == "cat says 'howl'"; | ||
assert dog.says() == "dog says 'woof'"; | ||
} |