# Understanding the dangers of abusing multiple dispatch

**URL:** <https://discourse.julialang.org/t/understanding-the-dangers-of-abusing-multiple-dispatch/43301>\
**Category:** New to Julia\
**Created:** [July 18, 2020, 6:10pm UTC](https://discourse.julialang.org/t/understanding-the-dangers-of-abusing-multiple-dispatch/43301 "2020-07-18T18:10:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bryan\_So](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bryan_so/32/16228_2.png) [@Bryan\_So](https://discourse.julialang.org/u/Bryan_So)\
**Post date:** [July 18, 2020, 6:10pm UTC](https://discourse.julialang.org/t/understanding-the-dangers-of-abusing-multiple-dispatch/43301/1 "2020-07-18T18:10:25Z")

</div>

Hi, I’d like to understand more about this topic:

**The dangers of abusing multiple dispatch**  
[https://docs.julialang.org/en/v1.6-dev/manual/performance-tips/#The-dangers-of-abusing-multiple-dispatch-(aka,-more-on-types-with-values-as-parameters)-1](https://docs.julialang.org/en/v1.6-dev/manual/performance-tips/#The-dangers-of-abusing-multiple-dispatch-(aka,-more-on-types-with-values-as-parameters)-1)

I don’t understand when we will get combinatorial explosion, quote

> For example, you might imagine using it to store information, e.g.

```julia
struct Car{Make, Model}
    year::Int
    ...more fields...
end

```

> and then dispatch on objects like `Car{:Honda,:Accord}(year, args...)` .

> This might be worthwhile when either of the following are true:
> 
> - You require CPU-intensive processing on each `Car` , and it becomes vastly more efficient if you know the `Make` and `Model` at compile time and the total number of different `Make` or `Model` that will be used is not too large.
> - You have homogenous lists of the same type of `Car` to process, so that you can store them all in an `Array{Car{:Honda,:Accord},N}`
> 
> When these do not hold, then it’s likely that you’ll get no benefit; worse, the resulting “combinatorial explosion of types” will be counterproductive.

Given Car{Make, Model} I guess we have combinations like:

Car{:Honda, :Civic)  
Car{:Honda, :Accord)  
Car{:Toyota, :Prius)  
Car{:Toyota, :Corolla)

The combination should be Make x Model, which is polynomial (n\*m), not exponential (n^m)

I wonder if someone can give a more elaborated example of the combinatorial explosion mentioned in the linked article?

Thanks

---

<div class="post-metadata">

**Author:** ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)\
**Post date:** [July 18, 2020, 6:41pm UTC](https://discourse.julialang.org/t/understanding-the-dangers-of-abusing-multiple-dispatch/43301/2 "2020-07-18T18:41:57Z")

</div>

I think combinatorial explosion is an exaggeration. Technically the number of types is exponential in the number of parameters. The only practical example of this I can think of is if you try use tuples to store non homogeneous lists of objects. Anyway the manual is right that the car example is an anti pattern and will lead to slow code even if I would not call it combinatorial explosion.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [July 18, 2020, 7:54pm UTC](https://discourse.julialang.org/t/understanding-the-dangers-of-abusing-multiple-dispatch/43301/3 "2020-07-18T19:54:11Z")

</div>

The combinatorial explosion comes rather from something where each entry is independent, e.g.

`Car{:Honda, :3_wheeler, :hatchback, :goodyear, :green, :sunroof}`

---

<div class="post-metadata">

**Author:** ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)\
**Post date:** [July 18, 2020, 10:44pm UTC](https://discourse.julialang.org/t/understanding-the-dangers-of-abusing-multiple-dispatch/43301/4 "2020-07-18T22:44:01Z")

</div>

I think there are more issues here beyond just the exponential complexity in the number of parameters.

There’s also the issue that you might have to search through much of the potentially large method space to decide which method is most specific. The make/model example AFAICT doesn’t highlight this issue because there’s no depth to the type hierarchy and no ambiguities I’m seeing. The possibility of ambiguities makes dispatch more expensive than simply looking up methods in a hash table indexed by “types as enums”. And even that’s more expensive than a simple branch on something like `make == :Honda`.
