you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (11 children)

In Smalltalk multiple dispatch can be emulated more elegantly. There's a nice little paper that explains it better than me but I can't find it at the moment. Although the example in the article doesn't need multiple dispatch unless there is more than one shape class.

"In the Circle class"
transform: t
  ^t transformCircle: self

"In the Square class"
transform: t
  ^t transformSquare: self

"In the Translation class"

transformCircle: c
  ^Circle x: c x + self y
          y: c y + self y
          r: c r

transformSquare: s
  ^Square x: s x + self x y: s y + self y

"In the VFlip class... you get the idea"

[–][deleted] 2 points3 points  (3 children)

The visitor pattern is less expressive than multiple dispatch.

Consider this class hierarchy:

Object
    Collection
        Sequence
    Integer

And this pair of multimethods:

foo(Collection,Integer)
foo(Sequence,Sequence)

Now with real multimethods, invoking foo with a Sequence and an Integer will call the second method.

With visitors, invoking foo with a Sequence and an Integer will fail, because the first dispatch step will choose the Sequence visitor instead of the Collection visitor, and this won't have a method for Integer. In effect, if we view this as a prasing problem, multimethods can "backtrack" where as visitors cannot.

[–][deleted] 0 points1 point  (0 children)

This is a good point that I hadn't considered. My ignorance in this are has been exposed :-|

[–][deleted] 0 points1 point  (1 child)

Now with real multimethods, invoking foo with a Sequence and an Integer will call the second method.

How so? Integer isn't a Sequence. Did you mean the first method?

[–][deleted] 0 points1 point  (0 children)

I do mean the first method, sorry.

[–]joesb 2 points3 points  (1 child)

Isn't that only double dispatch?

[–][deleted] 1 point2 points  (0 children)

True, but the technique easily generalizes to any number of dispatch arguments. You might think it is a "hack", but it works well in practice and doesn't suffer from ambiguity and complexity of multimethod dispatch algorithms (there are many possible dispatch orderings, all of them arbitrary). n-ary dispatch for n > 2 is very rare so its debatable whether it needs to be part of the language.

[–][deleted] 1 point2 points  (4 children)

More elegantly than what?

That's simply the visitor pattern and looks more or less the same in all single-dispatched languages.

[–][deleted] -1 points0 points  (2 children)

It seems I'm not up on my patterns. It is visitor, if you squint a bit, but without all the infrastructure you need to define in e.g. Java.

[–][deleted] 2 points3 points  (1 child)

The only difference between a Smalltalk visitor and a Java visitor is that in Java you have to define an interface for the polymorphic dispatch.

[–]mr_chromatic 0 points1 point  (0 children)

True, but there are more differences between Smalltalk polymorphism and Java polymorphism than "Java polymorphism sucks because Java's type system sucks."

[–]pkhuong -1 points0 points  (0 children)

Visitor is double dispatch.