I prefer multiple dispatch to Visitor - for the same reasons as Attila
outlined above plus with pattern matching you can have closed world
problems (the so called 'expression problem'), poor error messages
(Scala open patterns), and/or runtime errors (Mathematica). These
problems with pattern matching might be fixable, but multiple dispatch
is a viable alternative, e.g. PEC (http://pec.dev.java.net/nonav/
compile/javadoc/pec/compile/multipledispatch/package-summary.html).
PEC solves all these problems by writing a dispatcher (I will write
more about its method resolution in a separate post).
The example in PEC is:
interface Visit implements MultipleDispatch { void visit( Object ); }
class AbstractTree implements Visit {
public final void visit( Object o ) {} // Body replaced by PEC
public final static void visit( Visit self, Map m ) { ... } // could
be in *any* class
public final static void visit( Visit self, List l ) { ... } //
could be in *any* class
}
(PEC adds no new syntax so that it can be used with existing tools, on
the other hand that means no nifty syntax.)
On Dec 8, 7:59 am, Attila Szegedi <[EMAIL PROTECTED]> wrote:
> On 2007.12.08., at 3:05, Werner Schuster (murphee) wrote:
>
>
>
>
>
> > Attila Szegedi wrote:
> >> Also, I'm talking solely about the problem of selecting among
> >> overloaded Java methods invoked from a dynamic language here. Dynamic
> >> languages are better off without a concept of an overloaded method
> >> altogether, as they can mostly have a single method accepting any
> >> types of arguments anyway :-)
>
> > ... which then has to do the pattern matching and dispatching itself,
> > and has to implement that itself.
>
> >http://www.artima.com/rubycs/articles/patterns_sexp_dslsP.html
> >http://www.ibm.com/developerworks/linux/library/l-pydisp.html
> >http://thinkpython.blogspot.com/2005/03/dylan-style-multiple-dispatch...
>
> > Just think of all the visitor implementations out there which would
> > become unnecessary with proper multi-dispatch...
>
> My personal experience is that if you want to do visitors with pattern
> matching, then Java-style overloaded methods don't buy you much in
> terms of code conciseness. Ultimately, if you have:
>
> void visit(Map)
> void visit(List)
>
> then your void visit(Object) will need to look like:
>
> if(obj instanceof Map) {
> visit((Map)obj);} else if(obj instanceof List) {
>
> visit((List)obj);
>
> } ...
>
> I find Scala's idea of case classes + pattern matching on them to be a
> much nicer fit for implementing this kind of patterns (even if it
> arguably can go against more orthodox encapsulation concepts, anyway,
> that's a different story).
>
> Attila.
>
> --
> home:http://www.szegedi.org
> weblog:http://constc.blogspot.com
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "JVM
Languages" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/jvm-languages?hl=en
-~----------~----~----~----~------~----~------~--~---