My package exposes this caller
method, whose behaviour may be controlled by consumers provided they specialize f
:
TestPkg/src/TestPkg.jl
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:
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([])
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:
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:
breaks user code:f(a) = println("default for $a (ignored kwargs)")
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:
fixes the warning without error, but it breaks the expected behaviour in a subtle way:f(a; b = 0) = println("default for $a (ignored kwargs)")
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?