# Dispatch on keywords

**URL:** <https://discourse.julialang.org/t/dispatch-on-keywords/129854>\
**Category:** New to Julia\
**Tags:** question, dispatch, keyword-arguments\
**Created:** [June 13, 2025, 9:25am UTC](https://discourse.julialang.org/t/dispatch-on-keywords/129854 "2025-06-13T09:25:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cyan](https://avatars.discourse-cdn.com/v4/letter/c/278dde/32.png) [@Cyan](https://discourse.julialang.org/u/Cyan)\
**Post date:** [June 13, 2025, 9:25am UTC](https://discourse.julialang.org/t/dispatch-on-keywords/129854/1 "2025-06-13T09:25:43Z")

</div>

I wrote these two functions

```julia
function test(;e::Float64)
    return e^2
end
function test(;e::Int)
    return e*2
end

```

I obtain the right answer when `test(1.)` or `test(1)`, but the `test(e=1.)` returns an error of:

```julia
TypeError: in keyword argument e, expected Int64, got a value of type Float64

```

What is wrong with the `test(e=1.)` ?

---

<div class="post-metadata">

**Author:** ![jakobnissen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jakobnissen/32/13477_2.png) [@jakobnissen](https://discourse.julialang.org/u/jakobnissen)\
**Post date:** [June 13, 2025, 9:29am UTC](https://discourse.julialang.org/t/dispatch-on-keywords/129854/2 "2025-06-13T09:29:58Z")

</div>

Keyword arguments do not participate in dispatch. So, when you define the second method, the first one is overwritten (as it has the same signature, disregarding keyword arguments)

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [June 13, 2025, 9:30am UTC](https://discourse.julialang.org/t/dispatch-on-keywords/129854/3 "2025-06-13T09:30:03Z")

</div>

These aren’t two functions: you’re overwriting the only method of the single function `test` because keyword arguments don’t participate to dispatch.

---

<div class="post-metadata">

**Author:** ![FerreolS](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ferreols/32/216854_2.png) [@FerreolS](https://discourse.julialang.org/u/FerreolS)\
**Post date:** [June 13, 2025, 9:51am UTC](https://discourse.julialang.org/t/dispatch-on-keywords/129854/4 "2025-06-13T09:51:07Z")

</div>

If you want to dispatch on keyword, you can use some kind of barrier function :

```julia
test(;e) = _test(e)

function _test(e::Float64)
    return e^2
end
function _test(e::Int)
    return e*2
end

```

```julia-repl
julia> test(e=1)
2

julia> test(e=1.)
1.0

```

---

<div class="post-metadata">

**Author:** ![danielmatz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielmatz/32/2285_2.png) [@danielmatz](https://discourse.julialang.org/u/danielmatz)\
**Post date:** [June 13, 2025, 2:08pm UTC](https://discourse.julialang.org/t/dispatch-on-keywords/129854/5 "2025-06-13T14:08:30Z")

</div>

There’s also the very nice KeywordDispatch.jl package.

---

<div class="post-metadata">

**Author:** ![matthias314](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@matthias314](https://discourse.julialang.org/u/matthias314)\
**Post date:** [June 13, 2025, 8:51pm UTC](https://discourse.julialang.org/t/dispatch-on-keywords/129854/6 "2025-06-13T20:51:34Z")

</div>

> [@jakobnissen](#):
>
> Keyword arguments do not participate in dispatch.

Is this really true? The following 10-year old issue still applies to Julia 1.11:

> <https://github.com/JuliaLang/julia/issues/9498>
>
> So this is an issue that I find after figuring out how the keyword arguments are… currently implemented in Julia. It will probably not be an issue anymore if #2773 is implemented.
> 
> The document on methods says
> 
> \> Methods are dispatched based only on positional arguments, with keyword arguments processed after the matching method is identified.
> 
> However, method dispatch actually behaves differently when doing a function call with or without keyword arguments. i.e.
> 
> \`\`\` julia
> julia\> function f(::Integer)
> 2
> end
> f (generic function with 1 method)
> 
> julia\> function f(::Number; kw...)
> 1
> end
> f (generic function with 2 methods)
> 
> julia\> f(1)
> 2
> 
> julia\> f(1; a = 2)
> 1
> \`\`\`
> 
> What happens here is that \`f.env.kwsorter\` only has one method defined and therefore when calling with keyword argument, \`f(::Integer)\` does not participate in method dispatch.
> 
> IMHO, there are several possible ways to fix it,
> 1. Fix the document to include this behavior. This should be the easiest fix but will probably make the whole keyword argument/optional argument/multiple dispatch more confusing especially for someone who does not know how it all works behind the scene. (It's already quite confusing/surprising that anonymous function does not support keyword argument for someone (like me) that expects python-like keyword argument implementation.)
> 2. Having an entry (that just throw an error) in \`env.kwsorter\` even for methods that does not take keyword arguments. This can also avoid the following confusing abuse of overriding method
>    
> \`\`\` julia
> julia\> function f(::Number; kw...)
> 1
> end
> f (generic function with 1 method)
>    
> julia\> function f(::Number)
> 2
> end
> f (generic function with 1 method)
>    
> julia\> f(1)
> 2
>    
> julia\> f(1; a = 2)
> 1
> \`\`\`
>    
> This is probably the easiest way to fix the code and is consistent with the best long term behavior.
> 3. Fix #2773 and let the method themselves handle keyword argument dispatch. Since #2773 is on \`1.0\` milestone, hopefully this will be eventually be implemented.

---

<div class="post-metadata">

**Author:** ![jakobnissen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jakobnissen/32/13477_2.png) [@jakobnissen](https://discourse.julialang.org/u/jakobnissen)\
**Post date:** [June 14, 2025, 8:34am UTC](https://discourse.julialang.org/t/dispatch-on-keywords/129854/7 "2025-06-14T08:34:01Z")

</div>

Yes, this is a bug.
