# Keyword argument method without overwriting original method

**URL:** <https://discourse.julialang.org/t/keyword-argument-method-without-overwriting-original-method/29927>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 15, 2019, 7:37am UTC](https://discourse.julialang.org/t/keyword-argument-method-without-overwriting-original-method/29927 "2019-10-15T07:37:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amin\_Yahyaabadi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amin_yahyaabadi/32/9826_2.png) [@Amin\_Yahyaabadi](https://discourse.julialang.org/u/Amin_Yahyaabadi)\
**Post date:** [October 15, 2019, 7:37am UTC](https://discourse.julialang.org/t/keyword-argument-method-without-overwriting-original-method/29927/1 "2019-10-15T07:37:33Z")

</div>

```julia
julia> fun(x::Integer) = 1
fun (generic function with 1 method)

julia> fun(x::Integer; y::Array) = 1
fun (generic function with 1 method)

julia> fun(x::Integer, y::Array) = 1
fun (generic function with 2 methods)

```

Why doesn’t the 2nd definition create another method?  
I know that I can make keyword optional by

```julia
julia> fun(x::Integer; y::Array= nothing) = 1
fun (generic function with 1 method)

```

But in my situation, I am expanding another function’s methods, and the function has `fun(x::Integer) = 1` originally, and I don’t want to overwrite that method.

---

<div class="post-metadata">

**Author:** ![Amin\_Yahyaabadi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amin_yahyaabadi/32/9826_2.png) [@Amin\_Yahyaabadi](https://discourse.julialang.org/u/Amin_Yahyaabadi)\
**Post date:** [October 15, 2019, 7:45am UTC](https://discourse.julialang.org/t/keyword-argument-method-without-overwriting-original-method/29927/2 "2019-10-15T07:45:46Z")

</div>

In the Slack group, It is pointed out that keyword arguments never participate in dispatch, and so there is no way other than redefining the original methods.

> <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.

> [@Rules for method dispatch with keyword arguments](https://discourse.julialang.org/t/rules-for-method-dispatch-with-keyword-arguments/23626/3):
>
> If I type it to Int then the method is overwritten. But the point is that the second method should have a different signature in the positional parameter space. I chose Any but equally well I could have written e.g.: f(x::Integer; kwarg) = "integer" and the same problem would arise. For a reference: this is not an artificial question but in DataFrames.jl we actually have methods which for “wider” (something like catch-all) method signature take keyword arguments but for narrower signatures di…

---

<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:** [October 15, 2019, 8:41am UTC](https://discourse.julialang.org/t/keyword-argument-method-without-overwriting-original-method/29927/3 "2019-10-15T08:41:35Z")

</div>

If you need this you can try

[https://github.com/simonbyrne/KeywordDispatch.jl](https://github.com/simonbyrne/KeywordDispatch.jl)

---

<div class="post-metadata">

**Author:** ![Amin\_Yahyaabadi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amin_yahyaabadi/32/9826_2.png) [@Amin\_Yahyaabadi](https://discourse.julialang.org/u/Amin_Yahyaabadi)\
**Post date:** [October 15, 2019, 9:32am UTC](https://discourse.julialang.org/t/keyword-argument-method-without-overwriting-original-method/29927/4 "2019-10-15T09:32:16Z")

</div>

Oh thanks! That is a nice package!

I redefined the methods using metaprogramming, but this is a nice package to consider.  
[https://github.com/juliamatlab/MatLang/blob/5f391ab3a0f5e0846d8bfdc1afc6e45685ec412d/src/Language\_Fundamentals/Matrices\_and\_Arrays.jl#L16-L116](https://github.com/juliamatlab/MatLang/blob/5f391ab3a0f5e0846d8bfdc1afc6e45685ec412d/src/Language_Fundamentals/Matrices_and_Arrays.jl#L16-L116)
