Last active
December 19, 2015 12:38
-
-
Save pnkfelix/5956068 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| trait A { fn foo(&self); } | |
| trait B<X:A> { fn bar(&self, @X); } | |
| trait C<X:A, Y:B<X>> { fn baz(&self, @X, @Y); } | |
| // This doesn't work, as trait bounds are not allowed in structure defn's.x | |
| // struct Cat<X:A, Y:B<X>> { x: @X, y: @Y } | |
| // Workaround by putting the check elsewhere | |
| struct Cat<X, Y> { x: @X, y: @Y } | |
| impl<X:A, Y:B<X>> C<X,Y> for Cat<X,Y> { | |
| fn baz(&self, x_arg: @X, y_arg: @Y) { | |
| println("baz"); | |
| self.x.foo(); | |
| x_arg.foo(); | |
| self.y.bar(x_arg); | |
| y_arg.bar(x_arg); | |
| } | |
| } | |
| struct Alpha(int); | |
| struct Beta(int); | |
| impl A for Alpha { fn foo(&self) { println("Alpha foo"); } } | |
| impl B<Alpha> for Beta { fn bar(&self, x:@Alpha) { println("Beta bar"); x.foo(); } } | |
| fn main() { | |
| let x = @Alpha(1); | |
| let y = @Beta(2); | |
| let cat : Cat<Alpha, Beta> = Cat::<Alpha, Beta> { x: @Alpha(3), y: @Beta(4) }; | |
| println(fmt!("Hello World %?", cat.baz(x, y))); | |
| } |
This file contains hidden or 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
| % rustc ~/Dev/Rust/foo.rs && ~/Dev/Rust/foo | |
| baz | |
| Alpha foo | |
| Alpha foo | |
| Beta bar | |
| Alpha foo | |
| Beta bar | |
| Alpha foo | |
| Hello World () |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment