Gregor Richards wrote:
I ran into a situation where I needed (essentially) the visitor pattern, but 
the visitor pattern sucks, so I wanted to make something like class extensions 
instead (that is, methods added to a class outside of the class definition).

Of course, it's not possible to do this particularly cleanly, but I made a 
system that works (albeit using gross string mixins). Essentially, if you have 
a class A, class B : A, class C : B, you could do something like this:

mixin(extensions("A", "void", "doFoo", "", ""));

mixin(extend("A", "doFoo"));
void A_doFoo(A pthis) {
    /* method for A */
}

mixin(extend("B", "doFoo"));
void B_doFoo(B pthis) {
    /* method for B */
}

Then if you call doFoo(new A()) the call will become A_doFoo(new A()), if you 
call doFoo(new B()) the call will become B_doFoo(new B()), if you call 
doFoo(new C()) the call will become B_doFoo(new C()).

If anybody has some improvements, that'd be cool. Maybe you can get rid of the 
dependence on string mixins ... but I don't think templates quite cut it.

 - Gregor Richards


Pretty cool stuff, but I don't see how this is at all better than the visitor pattern. It is not checked at compile-time (so if you forget to implement one part of the hierarchy, you won't find that out until runtime) and it's likely less efficient, especially for large enough hierarchies (i.e. syntax trees). Where's the happy?

Reply via email to