Dispatch on keywords

I wrote these two functions

function test(;e::Float64)
    return e^2
end
function  test(;e::Int)
    return e*2
end

I obtain the right answer when test(1.) or test(1), but the test(e=1.) returns an error of:

TypeError: in keyword argument e, expected Int64, got a value of type Float64

What is wrong with the test(e=1.) ?

Keyword arguments do not participate in dispatch. So, when you define the second method, the first one is overwritten (as it has the same signature, disregarding keyword arguments)

These aren’t two functions: you’re overwriting the only method of the single function test because keyword arguments don’t participate to dispatch.

If you want to dispatch on keyword, you can use some kind of barrier function :

test(;e) = _test(e)

function _test(e::Float64)
    return e^2
end
function  _test(e::Int)
    return e*2
end
julia> test(e=1)
2

julia> test(e=1.)
1.0

There’s also the very nice KeywordDispatch.jl package.

Is this really true? The following 10-year old issue still applies to Julia 1.11:

Yes, this is a bug.