# Method dispatch

**URL:** <https://discourse.julialang.org/t/method-dispatch/51709>\
**Category:** New to Julia\
**Created:** [December 12, 2020, 10:38am UTC](https://discourse.julialang.org/t/method-dispatch/51709 "2020-12-12T10:38:40Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ZettEff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zetteff/32/31976_2.png) [@ZettEff](https://discourse.julialang.org/u/ZettEff)\
**Post date:** [December 12, 2020, 10:38am UTC](https://discourse.julialang.org/t/method-dispatch/51709/1 "2020-12-12T10:38:40Z")

</div>

Julia is a lot about multiple dispatching and I am having trouble to understand what an idiomatic way of using mulitple dispatch is.

My question is for small methods if I should define two different methods or if I should just use if else statements? (i.e. `if typeof(...)...`)

Example: I want to format/convert numbers or arrays of numbers into a string. Is the below approach reasonable? Or what is an idiomatic way to deal with the two cases (i) a single number and (ii) an array of things (e.g. numbers).

```julia
function _format_params(para::Number)
    string(para)
end

function _format_params(para::Array)
    join(para,", ")
end

```

Thanks for explaining.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [December 12, 2020, 12:04pm UTC](https://discourse.julialang.org/t/method-dispatch/51709/2 "2020-12-12T12:04:53Z")

</div>

I think for really small methods with only 2 or 3 different types, it can be more convenient to use an if else statement, but just remember that in that case neither you nor any user can extend your method for their own types. So the set of types your code works with is more or less “locked”. That can be more or less problematic depending on the expected use of that code.

One other thing to keep in mind, if you dispatch on `Array` there’s a whole lot of AbstractArrays that wouldn’t work with that code, even though all array-like things should work in principle.

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [December 12, 2020, 1:33pm UTC](https://discourse.julialang.org/t/method-dispatch/51709/3 "2020-12-12T13:33:35Z")

</div>

If you code is type stable, multiple methods produce compile time dispatch (fast) while the branch is decided at run time (slow).

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [December 12, 2020, 3:26pm UTC](https://discourse.julialang.org/t/method-dispatch/51709/4 "2020-12-12T15:26:32Z")

</div>

> If you code is type stable, multiple methods produce compile time dispatch (fast) while the branch is decided at run time (slow).

At cost of a little more effort on the first time the multiple methods are run, and sligtly more memory use (as there are many versions of the same compiled code), there is no free lunch. However, this is often the trade-off you want to do, unless low latency is crucial.

---

<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:** [December 12, 2020, 8:22pm UTC](https://discourse.julialang.org/t/method-dispatch/51709/5 "2020-12-12T20:22:04Z")

</div>

> [@hendri54](#):
>
> while the branch is decided at run time (slow).

No, if the compiler can figure out the types, the unneeded branches are removed at compile time. So you will get the same performance.

Personally, though, I strongly prefer the multi-methods approach, also (or even _particularly_) for short methods like in the OP.

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [December 13, 2020, 10:17pm UTC](https://discourse.julialang.org/t/method-dispatch/51709/6 "2020-12-13T22:17:05Z")

</div>

Using a distinct function for each type signature (with functions that take multiple arguments, “for each signature”) makes it easier to think about your program. If you need to revisit it at a much later date, perhaps to expand its coverage (as mentioned above) or to enhance its logic, this style can simplify doing so. Even where one later needs to incorporate some additional step for each method of that function, refactoring to use an outer function would be better than facing editing a internally composite source.

It is best practice to avoid `if typeof(argument) == _` without some clear and strong reason to do so.

---

<div class="post-metadata">

**Author:** ![ZettEff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zetteff/32/31976_2.png) [@ZettEff](https://discourse.julialang.org/u/ZettEff)\
**Post date:** [December 14, 2020, 3:16am UTC](https://discourse.julialang.org/t/method-dispatch/51709/7 "2020-12-14T03:16:29Z")

</div>

Thanks to all of you for your answers. I conclude that there are three things to take into consideration (i) performance, (ii) readability and (iii) extensibility.

For all three using distinct function may be helpful or even better. Thanks!
