Created
June 22, 2013 18:36
-
-
Save MaikKlein/5842031 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 Visitor<T>{ | |
| fn visit(&self, t: T); | |
| } | |
| struct PrintVisitor; | |
| impl PrintVisitor { | |
| fn print(&self, s: ~str){ | |
| println(s); | |
| } | |
| } | |
| impl Visitor<~str> for PrintVisitor { | |
| fn visit(&self, s: ~str){ | |
| println(s); | |
| } | |
| } | |
| struct PrintReverseVisitor; | |
| impl PrintVisitor { | |
| fn print(&self, s: ~str){ | |
| println(s); | |
| } | |
| } | |
| impl Visitor<~str> for PrintReverseVisitor { | |
| fn visit(&self, s: ~str){ | |
| let x: ~[char] = s.rev_iter().collect(); | |
| println( std::str::from_chars(x)); | |
| } | |
| } | |
| struct SomeObject; | |
| impl SomeObject { | |
| fn do_sth<'r>(&self, v: &'r Visitor<~str>, s: ~str){ | |
| v.visit(s); | |
| } | |
| } | |
| fn main(){ | |
| let v = &PrintVisitor; | |
| let v1 = &PrintReverseVisitor; | |
| let o = SomeObject; | |
| o.do_sth(v as &Visitor<~str>, ~"Hello"); | |
| o.do_sth(v1 as &Visitor<~str>, ~"Hello"); | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment