8

I have never written software in Common Lisp, but in Scheme and Clojure as well as C++ and Python. Yet I have had a look at the Common Lisp Object System (CLOS) in Common Lisp and Dylan. Now when comparing it to OOP in the Simula tradition, CLOS and lets say C++ and Python class system have polymorphism as a common trait.

They seem to differ in the way how data and methods are organized, in the Simula family, methods are an integral part of classes, sub-items if you will while with CLOS they are defined outside of classes and are rather generalized procedures.

Common Lisp proponents typically argue that virtual methods in Simula-family programming languages are quite limited since they are invoked only according to the first-argument object (this or self). CLOS generic functions are selected for several arguments, thus something like (pseudo code)

method draw(c : Device, d : Drawable) ... 

can be implemented for arbitrary Canvas and Drawable combinations.

method draw(c : Printer, t : Triangle)....
method draw(c : Printer, t : Image)....
method draw(c : Screen, t : Image) ....

In Simula-family languages, this would be more difficult, since draw would have to be associated with either the Device or the Drawable family of objects. The canonical solution seems to be the Visitor Pattern http://en.wikipedia.org/wiki/Visitor_Pattern .

Upon learning CLOS, I was quite amazed that most OO programming languages do not offer multiple dispatch - and - wondered if there are specific reasons, why one might associate methods so closely with one specific class. Are there specific advantages for single dispatch?

  • ambiguity of multiple dispatch: i.e. lets say I have a subclassing A and b subclassing B. The method foo(A,B) implemented as foo(a,B) and foo(A,b), which implementation would a call of foo(a,b) invoke? Is this the only reason?
  • "Modularizing", methods in the class are near.

Is there more to it? Have language designers and creators taken a stance on this matter?

1
  • The Go Language offers implicit interfaces, which seem to have some of the capabilities of multiple dispatch. Commented Aug 27, 2013 at 19:53

3 Answers 3

3

Single dispatch is somewhat easier to implement, and multiple dispatch is rather rarely needed in practice according to research by Muschevici et al.

You can find more information in the Wikipedia article on multiple dispatch; for instance, it also explains why the CLOS method of defining multimethods may be a better fit for Lisp's extremely uniform syntax, rather than for the more prevalent nesting of methods within classes.

5

There are now several languages which support multiple dispatch. There are extensions for some. Perl/Moose, Dylan, Julia, ...

Single dispatch object-oriented language have some things which can be seen as an advantage:

  • a class can be a namespace. In CLOS classes are not namespaces.

  • methods can be in a class namespace. In CLOS methods are not in a class namespace. Thus to have shorter access to variables and accessors, CLOS uses macros WITH-SLOTS and WITH-ACCESSORS.

  • in a class/message-passing system, all messages can be forwarded to another object with a single action.

  • a class could have a handler for all messages it does not understand. In CLOS this would look a bit more difficult.

  • performance may be a problem in a CLOS-like system due to the mix of multi-methods and multiple-inheritance.

  • source code organization and finding methods could be more difficult or at least be different in a CLOS like system.

2

Beyond "modularity", a consequence of a method associated (or belonging) to an object is its autonomy to chose how to respond to a message (and to "travel" with its own behavior). With multi dispatch as found in these languages, you have an underlying global mechanism doing the binding and deciding how computation is carried out.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.