# Why is multiple dispatch a feature?

**URL:** <https://discourse.julialang.org/t/why-is-multiple-dispatch-a-feature/31101>\
**Category:** Internals & Design\
**Tags:** question\
**Created:** [November 14, 2019, 9:06pm UTC](https://discourse.julialang.org/t/why-is-multiple-dispatch-a-feature/31101 "2019-11-14T21:06:19Z")\
**Posts on this page:** 14\
**Page:** 2

<div class="post-metadata">

**Author:** ![Sayod](https://avatars.discourse-cdn.com/v4/letter/s/b3f665/32.png) [@Sayod](https://discourse.julialang.org/u/Sayod)\
**Post date:** [November 15, 2019, 9:27am UTC](https://discourse.julialang.org/t/why-is-multiple-dispatch-a-feature/31101/21 "2019-11-15T09:27:11Z")

</div>

> [@yuyichao](#):
>
> No, it only means methods/verbs that doesn’t belong to an object can’t be namespaced. In another word, it means that you have to have your problem when you write something that’s hard to express without multiple dispatch. For things that works for you in other languages there doesn’t have to be a difference.

I am not sure if I understand what you are saying, so I am going to rephrase it so that you can correct me: You are saying that for things that work in other language (i.e. normal methods) there does not have to be a difference, i.e. you could use normal methods in these cases which could be namespaced and only use the multiple-dispatch like methods when needed?

Because I don’t get what you want to say with this. There aren’t really two different types of methods in julia, are there?

And this sounds a bit like the difference between functions and methods and not necessarily multiple-dispatch methods and methods. I.e. you have functions which don’t belong to anything and methods which belong to an object. But I also don’t really get what the difference between functions and multiple dispatch is…

Well except for the fact that you would write the distinction of cases for different types in the function body, while it is spread over different objects in the multiple dispatch case. So I guess in a sense it is a global function @override decorator.

---

<div class="post-metadata">

**Author:** ![antoine-levitt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/antoine-levitt/32/4008_2.png) [@antoine-levitt](https://discourse.julialang.org/u/antoine-levitt)\
**Post date:** [November 15, 2019, 9:58am UTC](https://discourse.julialang.org/t/why-is-multiple-dispatch-a-feature/31101/22 "2019-11-15T09:58:16Z")

</div>

Julia IDEs just don’t have the massive history and funding that have gone into Java editors. This is largely orthogonal to multiple dispatch vs OOP. Just because nobody has made a nice and convenient UI for discovery of methods doesn’t mean it can’t be done, it just means that nobody has done it.

You could imagine writing `obj` plus some magic key combination would prompt an UI allowing you to select the method and would rewrite it as `method(obj)`. Maybe there’ll be a PR proposing `obj1:obj2(method)` as an alias for `method(obj1,obj2)` or some crazy syntax like that, and then you’d just get standard REPL completion. There’s already `|>` which allows you to do eg `randn(2,2) |> qr`. Tab completion doesn’t restrict to the correct methods right now, but that’s probably a 10-lines PR away. There is a large design space that is still unexplored just because multiple dispatch languages are less “standard” than OOP.

Also there’s a huge difference in communities: I know many people that don’t particularly like Java-style IDEs and would definitely object to

> And there aren’t that many vim users.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [November 15, 2019, 10:39am UTC](https://discourse.julialang.org/t/why-is-multiple-dispatch-a-feature/31101/23 "2019-11-15T10:39:28Z")

</div>

> [@Sayod](#):
>
> This discoverability feature is itself not very discoverable.

I don’t think it’s too big a hurdle to have to learn a shortcut to trigger method discovery. You also have to learn that you have to write `.` in order to access properties and methods in OO.

Unless everyone is perpetually a first time user of Julia, this is not a big deal.

Also, I don’t see how this is an issue with multiple dispatch specifically. Isn’t this just how it is with _every_ paradigm except class-based OO?

Singular dispatch is simply a big giant annoyance every time I want behaviour to depend on more than one single input argument type. If could get back of half the time I’ve spent coding input parsing, I would probably have accomplished great things in my life 😃 Multiple dispatch solves this elegantly.

Furthermore, external methods allow me to add behaviour to types without having to change other peoples code. These features are huge steps forward, in my opinion.

---

<div class="post-metadata">

**Author:** ![jonathanBieler](https://avatars.discourse-cdn.com/v4/letter/j/82dd89/32.png) [@jonathanBieler](https://discourse.julialang.org/u/jonathanBieler)\
**Post date:** [November 15, 2019, 11:09am UTC](https://discourse.julialang.org/t/why-is-multiple-dispatch-a-feature/31101/24 "2019-11-15T11:09:56Z")

</div>

I actually have a (somewhat buggy) version of this since a few years :

![30](https://global.discourse-cdn.com/julialang/original/3X/2/2/22afae3c6ad7983b7a504038f6b09f10bea622b3.gif)

![43](https://global.discourse-cdn.com/julialang/original/3X/2/6/26e2dd2a3a1a95721480c892179c5d5d41a474ac.gif)

I don’t use it much though, probably because it doesn’t work well enough.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 15, 2019, 12:05pm UTC](https://discourse.julialang.org/t/why-is-multiple-dispatch-a-feature/31101/25 "2019-11-15T12:05:23Z")

</div>

> [@Sayod](#):
>
> I.e. you have functions which don’t belong to anything and methods which belong to an object. But I also don’t really get what the difference between functions and multiple dispatch is…

This is all documented in detail:

[https://docs.julialang.org/en/v1/manual/methods/](https://docs.julialang.org/en/v1/manual/methods/)

but in case of TL;DR:

- a function is a collection of methods, usually organized by a name (but there are anonymous functions),

- multiple dispatch is the mechanism that looks up the method to use from this collection

- methods don’t “belong to” any object, but they “belong to” a function in the sense above

- namespaces are completely orthogonal to this.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [November 15, 2019, 12:27pm UTC](https://discourse.julialang.org/t/why-is-multiple-dispatch-a-feature/31101/26 "2019-11-15T12:27:55Z")

</div>

> [@DNF](#):
>
> If could get back of half the time I’ve spent coding input parsing, I would probably have accomplished great things in my life 😃

Just an update to this: I am, _right now_, struggling with how handle various combinations of inputs to a Matlab plotting function for a class I’m writing. Is the axis handle specified? Are plot ranges given? What about a preprocessor? It is _infuriating_. Singular dispatch 🤬!! I’d give an arm and a leg to be allowed to transition to a multiple dispatch language (i.e. Julia).

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [November 15, 2019, 12:45pm UTC](https://discourse.julialang.org/t/why-is-multiple-dispatch-a-feature/31101/27 "2019-11-15T12:45:34Z")

</div>

> [@Sayod](#):
>
> i.e. you could use normal methods in these cases which could be namespaced and only use the multiple-dispatch like methods when needed?

No. I’m saying multiple dispatched function could be namespaced as well.

> [@Sayod](#):
>
> There aren’t really two different types of methods in julia, are there?

That is correct. I’m just saying that what you are complaining about isn’t about multiple dispatch at all.

---

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [November 15, 2019, 12:47pm UTC](https://discourse.julialang.org/t/why-is-multiple-dispatch-a-feature/31101/28 "2019-11-15T12:47:00Z")

</div>

On functions and methods: try playing with the REPL.

This defines the function `somefunction` with no methods:

```julia
julia> function somefunction end
somefunction (generic function with 0 methods)

```

This defines a method of `somefunction`, that dispatches on `Int` and `String` args. So multiple dispatch.

```julia
julia> somefunction(x::Int, y::String) = string(x, y)
somefunction (generic function with 1 method)

```

Now say you defined these in a module - Module1.

In some other package you want to define a new type and add a method for it to `somefunc`

```julia
module Module2

using Module1

struct NewType end

Module1.somefunction(x::Int, y::NewType) = string(x, "some string")

end

```

Namespacing, multiple dispatch, methods and functions.

Other packages just call `Module1.somefunction(2, NewType())`, and will work with your new custom type from Module2. This makes clean package interop ridiculously easy, and greatly reduces the total number of methods we need, as you can keep reusing the same ones from Base.

Yes its harder to auto complete method names than oop. But there are far less to remember anyway and namespacing is much less of a problem.

---

<div class="post-metadata">

**Author:** ![klaff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/klaff/32/7637_2.png) [@klaff](https://discourse.julialang.org/u/klaff)\
**Post date:** [November 15, 2019, 2:15pm UTC](https://discourse.julialang.org/t/why-is-multiple-dispatch-a-feature/31101/30 "2019-11-15T14:15:39Z")

</div>

I’ve been learning Julia for roughly six months, coming from (mostly) python and C. Multiple dispatch is definitely growing on me. I don’t feel as much oscillation between using classes and using more imperative style.

A simple example from yesterday: I was using `hcubature` (Thanks @stevengj!!) and it likes a function with a signature like `f(x)` where x is a vector, but I was also using Plots, which, if I want to write `plot(x,y,f, st=:surface)`, expects `f(x,y)`, where `x` and `y` are the two scalar args which would have been packed into the vector `x`. I started to use an anonymous function, but instead did this:

```julia
f((x,y)) = some expression # takes a single argument and unpacks it into the two scalars
f(x,y) = f((x,y)) # directly takes the two scalars

```

and boom, I can either use f with separate scalar arguments or with a vector of args without doing something like `f_for_hcube` and `f_for_plot`, or making a class with two different methods like `f.for_plot` or `f.for_hcube`.

It’s a tiny example, but it kept the function definition simple and clean as well as the code that calls it.

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [November 15, 2019, 2:51pm UTC](https://discourse.julialang.org/t/why-is-multiple-dispatch-a-feature/31101/31 "2019-11-15T14:51:45Z")

</div>

> [@yuyichao](#):
>
> Multiple dispatch does have many other nice aspect and that’s what’s some people are selling. It doesn’t mean it’s flawless.

Others have said this in various ways, but I just want to emphasize this point from yuyichao. Multiple dispatch is a language feature, not UI feature. The construction of the language does make some UI features harder or impossible to do, and that’s certainly a trade-off one has to consider, but it’s really a different thing. And, speaking as someone that used python exclusively before this, I can honestly say that I haven’t missed it much in ~3 years using mostly julia.

> [@Sayod](#):
>
> I am a Maths (especially Probability Theory and Statistics) student,

Others have already talked about the usefulness of MD for extensibility, and I’ll add my endorsement to Stefan’s video linked above, but given your a math person, I should think multiple dispatch should make a lot of sense to you. Thinking about addition, is there any reason why `sum(x, y)` should belong to `x` rather than `y`?

Even plots, which you mentioned are the same way. Why should the `plot` function belong to the xaxis argument vs the yaxis argument? If I’m writing a new vector-like type, do I really want to have to re-write all of the different plot functions (scatter, barplot, boxplot etc) for my type, or do I just want to tell the language to treat it like an array and have dispatch take care of it for me?

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [November 15, 2019, 2:57pm UTC](https://discourse.julialang.org/t/why-is-multiple-dispatch-a-feature/31101/32 "2019-11-15T14:57:39Z")

</div>

I have to say, coming from C++ (and a little Python) and also going through losing the autocomplete feature of `.` or `->`, I am _shocked_ by how big a deal people make out of this (granted, it’s not that many people that I’ve seen complaining about it, but many of those who do make it sound like they were asked to shoot their pet). I pretty much just forgot about it after a couple of weeks and never looked back, I can’t even say I was particularly heartbroken to have lost it in the first place. Part of it is that I just don’t think this way anymore: I’m now much more likely to want to know `methods(f)` (which I can simply type into the REPL) than which `struct`s are accepted as the first argument to `f`. It also seems to me that, as has already been pointed out, if you are sufficiently determined you could just build tooling to give you exactly the same tooling for this sort of discovery in Julia, though granted said tooling would be more complicated than it would be for most OO languages.

> [@Sayod](#):
>
> You can pass different objects to it? So why not just call it a function in those cases instead of a method? (not belonging to an object?). You just need the concept of an `interface` like you have in Java, and you could pass anything which implements that interface into the function.

Interfaces and dispatch can be orthogonal concepts. Sometimes you want dispatch to select for you between different objects with completely different interfaces. This is not uncommon. For me the most prominent example is that, when I work on IO-related code, I very often need to write functions that accept either `IO` objects or buffers (`Vector{UInt8}`) as arguments. These two things have completely different interfaces for good reason. With multiple dispatch it is trivially easy for me to write generic code that works for _either_ of these very different objects, even if all of the inner code that deals with them directly must be different.

I’ve said this before, but multiple dispatch as a central paradigm really is the ultimate killer feature of Julia to me. Julia does quite a lot of things right, but to me, this more than anything else is the thing that keeps me wanting to use Julia pretty much exclusively. Class-based OOP has seemed horribly broken to me since about a month after starting to use Julia, and, interestingly I don’t think I would have walked away with that perception of it had I not spent so many years on strictly class-based OOP languages.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 15, 2019, 3:47pm UTC](https://discourse.julialang.org/t/why-is-multiple-dispatch-a-feature/31101/33 "2019-11-15T15:47:27Z")

</div>

> [@ExpandingMan](#):
>
> I’m now much more likely to want to know `methods(f)` (which I can simply type into the REPL) than which `struct` s are accepted as the first argument to `f` . It also seems to me that, as has already been pointed out, if you are sufficiently determined you could just build tooling to give you exactly the same tooling for this sort of discovery in Julia, though granted said tooling would be more complicated than it would be for most OO languages.

Given that the position of arguments is rarely relevant _per se_ for method discovery, `methodswith` should be perfect for this purpose.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [August 23, 2020, 4:55am UTC](https://discourse.julialang.org/t/why-is-multiple-dispatch-a-feature/31101/35 "2020-08-23T04:55:23Z")

</div>

No not necessarily. Universal function call syntax would basically kill the possibility of having any namespace.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [August 23, 2020, 6:10am UTC](https://discourse.julialang.org/t/why-is-multiple-dispatch-a-feature/31101/36 "2020-08-23T06:10:23Z")

</div>

Please don’t review old topics for new suggestions. You may want to start a new one, but first kindly read

> [@PSA: Julia is not at that stage of development anymore](https://discourse.julialang.org/t/psa-julia-is-not-at-that-stage-of-development-anymore/44872):
>
> You heard about this brand new exciting programming language called Julia and have some suggestions for how it should work. Wonderful! We’re happy to have you here and help you learn the language and listen to your ideas! However, please be aware that “new” is a relative term. Yes, Julia is newer than, say, C, which was first created in 1972 and standardized in 1989. It is not, however, new in the sense that it is still being freely designed and changed in arbitrary ways. Julia development start…

[Previous page](https://discourse.julialang.org/t/why-is-multiple-dispatch-a-feature/31101.md?page=1)
