2
$\begingroup$

I'm trying to get my head around the difference between message passing (with which we are all familiar as by far the most common form of object orientation today) vs generic functions as implemented in CLOS (Common Lisp Object System).

In what sense is it possible to say java has a kind of multiple-dispatch system due to method overloading?

To sketch my understanding, in message passing you might do:

tell obja move objb
tell objb move obja

but where the function is first class and not in any object, you might do:

move obja objb

The part I do think I understand is that with generics there is no dilemma on where to place a function, whereas in message passing you may have to arbitrarily choose which class to put a function on, then just remember which one you put it on. With message passing, if you want to be able to call the behaviour from either, you have to duplicate the code (yes I know there are ways round that, but that's not the point).

I'm not sure I'm really getting the rest of how generics are different yet.

I just found a key reference on this point.

The wiki page on CLOS is also very good.

So, maybe best method is to hold off with the theory and just practice with clojure multi-methods at some point... unless anyone wishes to chip in.

$\endgroup$
  • $\begingroup$ I think this is language-specific enough to be off-topic, here at Computer Science. $\endgroup$ – David Richerby Aug 9 at 12:16
  • $\begingroup$ Idea - If you used one class to be a global container of all methods, (which would actually have to be static functions in this case), you could simulate multiple dispatch. You would always pass as the first parameter to any 'method' on this god object the first object concerned (in the single dispatch world this would be the class it belonged to), but then all the remaining parameters too could cause dispatch to different method bodies based on their type, via method overloading, so you'd have multiple dispatch, allbeit in very limited form (and barely OO any more perhaps, because static)... $\endgroup$ – mwal Aug 11 at 23:36
0
$\begingroup$

In what sense is it possible to say java has a kind of multiple-dispatch system due to method overloading?

In no sense. What Java's method overloading gives you is dynamic dispatch, not multiple dispatch.

The most commonly seen example of multiple dispatch in Java is its event listener system, where you can register multiple listeners which will all be triggered when an event occurs.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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