# Fun with kwargs methods

**URL:** <https://discourse.julialang.org/t/fun-with-kwargs-methods/15711>\
**Category:** General Usage\
**Created:** [September 30, 2018, 9:55pm UTC](https://discourse.julialang.org/t/fun-with-kwargs-methods/15711 "2018-09-30T21:55:38Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)\
**Post date:** [September 30, 2018, 9:55pm UTC](https://discourse.julialang.org/t/fun-with-kwargs-methods/15711/1 "2018-09-30T21:55:38Z")

</div>

Consider

```julia
julia> bar(x, y) = x / y
bar (generic function with 1 method)

julia> bar(x, y; c) = x + y + c
bar (generic function with 1 method)

julia> bar(3,4,c=5)
12

julia> bar(3,4)
ERROR: UndefKeywordError: keyword argument c not assigned
Stacktrace:
 [1] bar(::Int64, ::Int64) at ./REPL[2]:1
 [2] top-level scope at none:0

```

However (restarting Julia):

```julia
julia> bar(x, y; c) = x + y + c
bar (generic function with 1 method)

julia> bar(x, y) = x / y
bar (generic function with 1 method)

julia> bar(3,4)
0.75

julia> bar(3,4, c=5)
12

```

Is there a way to have the order not matter when creating two methods, one that requires a kwarg and one that doesn’t?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [September 30, 2018, 10:20pm UTC](https://discourse.julialang.org/t/fun-with-kwargs-methods/15711/2 "2018-09-30T22:20:44Z")

</div>

[Keyword arguments affect methods dispatch · Issue #9498 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/9498), it’s a method override.

---

<div class="post-metadata">

**Author:** ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)\
**Post date:** [September 30, 2018, 10:36pm UTC](https://discourse.julialang.org/t/fun-with-kwargs-methods/15711/3 "2018-09-30T22:36:53Z")

</div>

Thanks, @yuyichao. I hope this can be fixed soon.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [September 30, 2018, 10:48pm UTC](https://discourse.julialang.org/t/fun-with-kwargs-methods/15711/4 "2018-09-30T22:48:12Z")

</div>

Note that “fixing it” will likely NOT mean it’s order independent. The only issue here is the inconsistency between method overwrite and (non-)dispatch sensitivity of keyword argument.

The fix should be either make dispatch fully aware of keyword argument and make this a true partial overwrite (similar to what would happen if you define a method with optional argument) and keep the behavior you observe. Or the fix could be making this a full overwrite instead of a partial one leaving both the keyword and non-keyword method visible even though there are only supposed to be one method from the dispatch POV.

The order dependency you observe is caused by the overwrite, which is real and isn’t fixable.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [September 30, 2018, 10:51pm UTC](https://discourse.julialang.org/t/fun-with-kwargs-methods/15711/5 "2018-09-30T22:51:39Z")

</div>

I guess there could be a variance of making dispatch fully keyword aware that treat required keyword argument in a special way so that this won’t be an overwrite anymore. I’m not sure how easy it is to implement this behavior or making dispatch keyword aware though…

---

<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:** [August 23, 2019, 4:15am UTC](https://discourse.julialang.org/t/fun-with-kwargs-methods/15711/6 "2019-08-23T04:15:11Z")

</div>

So has this problem any solution?  
I want to define two methods (or 1 method if possible) as follows:

```julia
zerosM(dim::Integer) = zeros(dim, dim)
zerosM(dim::Integer; like::Array) = zeros(eltype(like), dim, dim)

```

How can I do this? `like` is optional.

---

<div class="post-metadata">

**Author:** ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)\
**Post date:** [August 23, 2019, 4:48am UTC](https://discourse.julialang.org/t/fun-with-kwargs-methods/15711/7 "2019-08-23T04:48:07Z")

</div>

```julia
zerosM(dim; like::AbstractArray=Vector{Float64}()) = zeros(eltype(like), dim, dim)

```

should do what you want.

---

<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:** [August 23, 2019, 5:15am UTC](https://discourse.julialang.org/t/fun-with-kwargs-methods/15711/8 "2019-08-23T05:15:06Z")

</div>

> [@Fun with kwargs methods](https://discourse.julialang.org/t/fun-with-kwargs-methods/15711/7):
>
> zerosM(dim; like::AbstractArray=Vector{Float64}()) = zeros(eltype(like), dim, dim) should do what you want.

Thank you! For this case that we know that the default `eltype` of `zeros()` is `Float64`, this works!

For other situations, we came up with this in Slack group which does the dispatch using `if else`:

```julia
zerosM(dim::Integer; like::Union{Array,Nothing}=nothing) = like === nothing ? zeros(dim,dim) : zeros(eltype(like), dim, dim)

```
