Suppose a function fun()
with keywords is overloaded like this:
function fun(; a::Real)
return a
end
function fun(; a::Vector)
return a
end
When executing the following code, I get a type error: TypeError: in keyword argument a, expected Vector, got a value of type Int64
:
fun(1) # TypeError
fun([1, 2])
But when the function fun()
gets normal arguments, i.e.:
function fun(a::Real)
return a
end
function fun(a::Vector)
return a
end
Then executing of this code snippet here:
fun(1) # -> 1
fun([1, 2]) # -> [1, 2]
works.
Keyword arguments are not overloadable. In other words, they do not participate in method dispatch---only positional arguments participate in dispatch. When you define the second method, f(; a::Vector)
, it overwrites the first method:
julia> function fun(; a::Real)
return a
end
fun (generic function with 1 method)
julia> function fun(; a::Vector)
return a
end
fun (generic function with 1 method)
julia> methods(fun)
# 1 method for generic function "fun":
[1] fun(; a) in Main at REPL[2]:1