Skip to content

Instantly share code, notes, and snippets.

@MaikKlein
Created June 22, 2013 14:49
Show Gist options
  • Save MaikKlein/5841146 to your computer and use it in GitHub Desktop.
Save MaikKlein/5841146 to your computer and use it in GitHub Desktop.
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+" World");
}
}
struct SomeObject;
impl SomeObject {
fn do_sth<'r>(&self, v: &'r Visitor<~str>, s: ~str){
v.visit(s);
}
}
fn main(){
let v = &PrintVisitor;
let o = SomeObject;
o.do_sth(v as &Visitor<~str>, ~"Hello"); // do I really have to cast?
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment