On Thu, 27 May 2004, Robert Klemme wrote:

> Well, my code wasn't meant to be a replacement for yours.  I just wanted to
> see how a quick hack of MD could look like.  I'm aware that these are
> different pairs of shoes with differnt requirements.  Although, from an
> abstract perspective Visitor is a special case of Double Dispatch which in
> turn is a special case of Multiple Dispatch. :-)
> 
> Visitor has its merits but nevertheless I don't like it very much.  On one
> hand you retrofit functionality to a class hierarchy but on the other hand
> you need this class hierarchy's collaboration to make it happen if you use
> visitor.  I find this a bit too intertwined.  I prefer the "hash lookup with
> class" approach.  If you do that inside the visitor, you don't need the
> visitee to call any visit_<klass> methods and your visitor code is
> completely independend from the class hierarchy you want to visit.  It seems
> to me to be a better separation of concerns.
> 

It was designed as far as I can tell for use with compilers, that is where it 
mostly frequently appears.  In that case I think it does a very good job as you 
have a number of passes you need to perform across your AST which hopefully is 
well structured into a nice class hiearchy.  That way you can generate code for 
a AddOp, a BinaryComputeIsh, or some such hiearchy like that, catching what you 
need where.  An AST is something that is well suited to a very deep class 
hiearchy and so the Visitor excels at processing it.  As for other applications, 
I'm not sure if it's as effective there.

> If you view it as a way of adding methods to a class, then it's clear that
> you will need it more seldom in Ruby than in compiled languages, because you
> can dynamically add methods by either defining new methods in a class,
> defining singleton methods or including modules.
> 

Right, I'm aware of that.  Actually it is interesting that C# has something akin 
to open class definitions to facilitate generated code.  I haven't checked to 
see if all file need to be present for the compile phase or if it can add 
additional functionality later, would be interesting to see.  

However when you can do that alot of the code winds up looking more like chain 
of responsability then Visitor.  Oh well I guess a number of the patterns are 
all dependent on how you view them.  

> Thanks for the nice and interesting discussion!
> 

No no, thank you :)

Charles Comstock