# What is the motivation for Julia not dispatching on keyword arguments?

**URL:** <https://discourse.julialang.org/t/what-is-the-motivation-for-julia-not-dispatching-on-keyword-arguments/117447>\
**Category:** General Usage\
**Tags:** question\
**Created:** [July 24, 2024, 10:43pm UTC](https://discourse.julialang.org/t/what-is-the-motivation-for-julia-not-dispatching-on-keyword-arguments/117447 "2024-07-24T22:43:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![TimHargreaves](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/timhargreaves/32/207284_2.png) [@TimHargreaves](https://discourse.julialang.org/u/TimHargreaves)\
**Post date:** [July 24, 2024, 10:43pm UTC](https://discourse.julialang.org/t/what-is-the-motivation-for-julia-not-dispatching-on-keyword-arguments/117447/1 "2024-07-24T22:43:00Z")

</div>

I have come to peace with the fact Julia does not dispatch on keyword arguments, but I’m still unsure as to why this is the case.

In [this](https://stackoverflow.com/questions/59674079/julia-multiple-dispatch-based-on-named-arguments) StackOverflow thread, a commenter suggests that keyword arguments do not participate in dispatch because this would lead to a combinatorial explosion in the number of methods generated.

I might be misunderstanding here, but to me, that seems like an argument for not _specialising_ on keyword arguments, rather than not dispatching.

I would have assumed that the number of methods is determined by how many the user writes.

Any thoughts on this topic would be appreciated.

---

<div class="post-metadata">

**Author:** ![Stephen\_Vavasis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stephen_vavasis/32/3389_2.png) [@Stephen\_Vavasis](https://discourse.julialang.org/u/Stephen_Vavasis)\
**Post date:** [July 24, 2024, 11:56pm UTC](https://discourse.julialang.org/t/what-is-the-motivation-for-julia-not-dispatching-on-keyword-arguments/117447/2 "2024-07-24T23:56:13Z")

</div>

I don’t know the answer to your question, but I discovered (by accident when my code behaved unexpectedly) that Julia does indeed sometimes dispatch on keyword arguments. See: [Keyword arguments affect methods dispatch · Issue #9498 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/9498)

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [July 25, 2024, 12:05am UTC](https://discourse.julialang.org/t/what-is-the-motivation-for-julia-not-dispatching-on-keyword-arguments/117447/3 "2024-07-25T00:05:56Z")

</div>

> [@TimHargreaves](#):
>
> I have come to peace with the fact Julia does not dispatch on keyword arguments, but I’m still unsure as to why this is the case.

I suspect that the main difficulty is that ordering method specificity becomes impossible in many cases with keyword arguments, so you would end up with lots more `MethodError: ambiguous` cases. The same issue doesn’t arise as often with positional arguments because you tend not to have large numbers of positional arguments in multi-method functions (if there are lots of parameters, you use keywords instead).

See also the discussion in [keyword arguments · Issue #485 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/485), where this feature was hashed out. @StefanKarpinski wrote:

> The main consideration here is making keyword arguments not completely impossibly complicated to understand in the presence of multiple dispatch.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [July 25, 2024, 7:23am UTC](https://discourse.julialang.org/t/what-is-the-motivation-for-julia-not-dispatching-on-keyword-arguments/117447/4 "2024-07-25T07:23:13Z")

</div>

> [@TimHargreaves](#):
>
> In [this](https://stackoverflow.com/questions/59674079/julia-multiple-dispatch-based-on-named-arguments) StackOverflow thread, a commenter suggests that keyword arguments do not participate in dispatch because this would lead to a combinatorial explosion in the number of methods generated.
> 
> I might be misunderstanding here, but to me, that seems like an argument for not _specialising_ on keyword arguments, rather than not dispatching.

Compare to the existing optional positional arguments and you’ll see that specialization isn’t involved yet, it’s entirely about multimethods with respect to arity:

```julia
(This isn't code, I just need formatting) 
foo(a=1,b=2) = ...
generates
foo() = foo(1, 2)
foo(a) = foo(a, 2)
foo(a,b) = ...

```

_n_ optional arguments make 1+_n_ methods instead of just the original `foo(a=1,b=2)` because they need to overwrite previous (or be overwritten by subsequent) `foo()` and `foo(a)` methods to prevent ambiguous dispatch of calls.  
Contrast that with hypothetical optional keyword arguments:

```julia
foo(;a=1,b=2) = ...
generates
foo() = foo(;a=1,b=2)
foo(;a) = foo(;a=a, b=2)
foo(;b) = foo(;a=1, b=b)
foo(;a, b) = ...

```

This short example doesn’t really show off the sum of combinations for space, but _n_ optional keyword arguments would make \sum\_{k=0}^{n} C(n, k)=2^n methods (2^_n_ grows slower than the (1+_n_)! in the linked comment, see binomial theorem to prove that equality). That gets real big real fast, and the only way to avoid making so many methods (to store and to compile for) is to not make multiple methods with respect to keyword arguments.

That’s just one of the implementation problems, but even if there weren’t any, I wouldn’t even want to dispatch over keyword arguments. Despite the earlier optional positional arguments example, it’s far more typical that a `foo(a)` method does not forward to `foo(a,b)`, but does something entirely different, e.g. `-x` vs `x-y`. On the other hand, keyword arguments basically exist to override a few select values in a large set of defaults, and that’s the only way writing arguments out of order has any convenience e.g. `plot(x, y; color="red")` does the same thing as `plot(x, y; linestyle="dots")`. Performance would still need specialization, and that does happen; reflection is harder but the underlying `kwcall` sorts the arguments into positional arguments and specializes over them.

---

<div class="post-metadata">

**Author:** ![TimHargreaves](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/timhargreaves/32/207284_2.png) [@TimHargreaves](https://discourse.julialang.org/u/TimHargreaves)\
**Post date:** [July 26, 2024, 1:36pm UTC](https://discourse.julialang.org/t/what-is-the-motivation-for-julia-not-dispatching-on-keyword-arguments/117447/5 "2024-07-26T13:36:09Z")

</div>

Ah, I hadn’t clicked that multiple methods were generated in this situation. Thank you for clarifying that!
