# Incorrect \`method overwritten\` warning with optional kwargs..?

**URL:** <https://discourse.julialang.org/t/incorrect-method-overwritten-warning-with-optional-kwargs/120714>\
**Category:** General Usage\
**Tags:** warning, kwargs, methods\
**Created:** [September 30, 2024, 9:29am UTC](https://discourse.julialang.org/t/incorrect-method-overwritten-warning-with-optional-kwargs/120714 "2024-09-30T09:29:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![iago-lito](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iago-lito/32/31924_2.png) [@iago-lito](https://discourse.julialang.org/u/iago-lito)\
**Post date:** [September 30, 2024, 9:29am UTC](https://discourse.julialang.org/t/incorrect-method-overwritten-warning-with-optional-kwargs/120714/1 "2024-09-30T09:29:52Z")

</div>

My package exposes this `caller` method, whose behaviour may be controlled by consumers provided they specialize `f`:

`TestPkg/src/TestPkg.jl`

```julia
module TestPkg

# Entry point for users.
caller(a) = f(a; b = 1)
export caller

# Extension point for users.
f(a; b = 0) = f(a)
f(a) = println("default for $a (ignored kwargs)")

end 

```

Here is how users opt into either the default or the specialized versions:

```jl
using TestPkg

TestPkg.f(a::Int) = println("specialized for $a without using b")
TestPkg.f(a::String; b = 0) = println("specialized for $a using b = $b")

caller(5)
caller("a")
caller([]) 

```

```julia
specialized for 5 without using b
specialized for a using b = 1
default for Any[] (ignored kwargs)

```

This behaviour is what I expect, but the following warning is issued:

```julia
WARNING: Method definition f(Any) in module TestPkg at ./TestPkg/src/TestPkg.jl:8 overwritten at ./TestPkg/src/TestPkg.jl:9.

```

I can’t find any satisfactory fix:

- Changing the extension point to:

```julia
f(a) = println("default for $a (ignored kwargs)")

```

breaks user code:

```julia
ERROR: LoadError: MethodError: no method matching f(::Int64; b::Int64)
Closest candidates are:
f(::Int64) got unsupported keyword argument "b"

```

- Changing the extension point to:

```julia
f(a; b = 0) = println("default for $a (ignored kwargs)")

```

fixes the warning without error, but it breaks the expected behaviour in a subtle way:

```julia
default for 5 (ignored kwargs) # <- /!\ Should be specialized.
specialized for a using b = 1
default for Any[] (ignored kwargs)

```

Is there any way out of this? If not, maybe the warning is a false positive in this situation?

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [September 30, 2024, 10:35am UTC](https://discourse.julialang.org/t/incorrect-method-overwritten-warning-with-optional-kwargs/120714/2 "2024-09-30T10:35:02Z")

</div>

Keyword arguments don’t participate in dispatch, so this is expected behavior. Ignoring keyword arguments could be done by providing a method taking arbitrary keyword args, like `f(a; kws...)` with `haskey(kws, ...)` checks in your provided fallback.

---

<div class="post-metadata">

**Author:** ![iago-lito](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iago-lito/32/31924_2.png) [@iago-lito](https://discourse.julialang.org/u/iago-lito)\
**Post date:** [September 30, 2024, 2:08pm UTC](https://discourse.julialang.org/t/incorrect-method-overwritten-warning-with-optional-kwargs/120714/3 "2024-09-30T14:08:55Z")

</div>

Thank you for feedback @Sukera. I understand that dispatch do not happen on keyword args, but using `f(a; kw...)` as a fallback breaks the observable behaviour here, because the first eventual `caller(5)` is not resolved to the specialized version (like in the second failed attempt in the OP).

So, it seems that I cannot fix the warning without breaking user code, right?

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [September 30, 2024, 2:54pm UTC](https://discourse.julialang.org/t/incorrect-method-overwritten-warning-with-optional-kwargs/120714/4 "2024-09-30T14:54:14Z")

</div>

What I’m saying is that dispatch wise, there is no difference between `f(a::T; b=...)` and `f(a::T)` for the same `T`. Julia simply does not make a difference between the two, so if a user of yours previously tried to distinguish methods like that (e.g. for `T == String`), they would have experienced broken or unexpected behavior, even if you could “fix” the warning on your end.

So, from that POV, I’m not sure there’s a way around breaking your API and provide a different way to opt into some more specialized version of your code.

---

<div class="post-metadata">

**Author:** ![iago-lito](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iago-lito/32/31924_2.png) [@iago-lito](https://discourse.julialang.org/u/iago-lito)\
**Post date:** [September 30, 2024, 2:56pm UTC](https://discourse.julialang.org/t/incorrect-method-overwritten-warning-with-optional-kwargs/120714/5 "2024-09-30T14:56:27Z")

</div>

Understood, the API was a mistake then. I’ll try to fix it instead of fixing the warning. Thank you : )
