9
$\begingroup$

Context

I've been programming in java for a few years now. And atm i'm learning something totally different: Clojure. There the expression problem can be solved by using multimethods whereas in java you will need something like the visitor pattern or something alike.

The question

I've been reading about multimethods and got quite confused with the actual difference with method overloading.

The only difference I spotted so far that a multimethod doesn't depend on the runtime type of the object on which the method is called.

$\endgroup$
2
  • $\begingroup$ In order to make this a general question (i.e. independent of concrete programming languages) you should include a short description of what "multimethods" are. $\endgroup$
    – Raphael
    Commented Sep 21, 2012 at 20:46
  • $\begingroup$ Probably related: stackoverflow.com/q/1801216/671013 $\endgroup$ Commented Oct 17, 2014 at 7:21

2 Answers 2

14
$\begingroup$

Overloading is when two or more methods have the same name but different signature (different argument types, different number of arguments). Overloading is resolved statically, depending only on the static types of the arguments. (The interaction of overloading and overriding, in Java, for example, makes the story a little more complicated). Overloading resolves statically to a particular method signature. Then at runtime, dynamic dispatch will select the most appropriate method with that signature.

Multimethods are a collection of methods that have the same name, the same number of arguments, and overlapping type signatures. Whenever a call is made to a multimethod (using the name of the multimethod), all of the methods in the collection is considered as a possible candidate for dispatch. The precise candidate is chosen depending on the runtime types of the arguments – the most specific method is selected. The actual runtime types of all arguments are used to determine which one to run, which differs from traditional single dispatch in Java, where only the first argument (the target) of the method is used to determine which method body is run.

$\endgroup$
11
  • $\begingroup$ In java for example I can do this in a class public void do(A a){//body 1} public void do(B b){//body 2}; Here they do differ based on the runtime types of the arguments $\endgroup$
    – tgoossens
    Commented Sep 21, 2012 at 20:00
  • 1
    $\begingroup$ The method that is selected will be based on the static type of the argument. Try it! $\endgroup$ Commented Sep 21, 2012 at 20:02
  • $\begingroup$ Ok. I'll give it a shot .. just to be sure :) $\endgroup$
    – tgoossens
    Commented Sep 21, 2012 at 20:02
  • $\begingroup$ I noticed that a major part in my confusion was in the fact that I lacked a decent understanding between dynamic vs static typed languages. (and strong vs weak typed) It makes sense now $\endgroup$
    – tgoossens
    Commented Sep 21, 2012 at 20:45
  • 1
    $\begingroup$ PS: I noticed that not this year, but next year i'll be having some courses from you :-) $\endgroup$
    – tgoossens
    Commented Sep 21, 2012 at 20:46
2
$\begingroup$

As far a as my understanding goes:

Multimethods are a generalization of method polymorphism.

A multimethod consists of 2 parts

  1. A name

  2. A dispatch method: which will produce a dispatch value

Based on this dispatch value, the appropriate method will be chosen. Overloading is a special case of multimethods where the dispatch method will return the static type as a dispatch value

In general, the dispatch method can be anything. It can return other values than the type.

$\endgroup$
6
  • $\begingroup$ This is a fairly faithful account of the story, in the sense that it generalises and unifies the way things happen. It's actually close to the notion of predicate dispatch, which is more general than multimethods. $\endgroup$ Commented Sep 23, 2012 at 6:23
  • $\begingroup$ BTW: You should encourage your classmates to use the Computer Science stackexchange. $\endgroup$ Commented Sep 23, 2012 at 7:03
  • $\begingroup$ Also, if you need a bachelor's project (Wetenschappelijke Vorming) in programming languages, send me an email. $\endgroup$ Commented Sep 23, 2012 at 8:20
  • $\begingroup$ @DaveClarke Glad to hear I got it right. At the moment I'm exploring really exciting stuff. It's the first time I try to step away a bit from OOP. Clojure has been great so far, but still a lot to learn. Multimethods for me have been a great way to solve the 'expression problem'. Nicely extendable (in contrast to the visitor pattern which just emulates double dispatch at a great cost of extendability). $\endgroup$
    – tgoossens
    Commented Sep 23, 2012 at 11:14
  • $\begingroup$ I'll sure try to encourage them ;-) $\endgroup$
    – tgoossens
    Commented Sep 23, 2012 at 11:15

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.