Failure to detect repeated names with splatted keyword args

Consider the following:

julia> kwargs = Dict(:a => 0)
Dict{Symbol, Int64} with 1 entry:
  :a => 0

julia> f(; a=1) = a
f (generic function with 1 method)

julia> f(; a=2, kwargs...)
0

julia> f(; a=2, a=0)
ERROR: syntax: keyword argument "a" repeated in call to "f"

Is that expected behavior, or should I report that as a bug? I would have expected f(; a=2, kwargs...) to throw the same error as f(; a=2, a=0). Is there a way to avoid this foot-gun, and have Julia detect duplicate keyword arguments when there’s splatting involved? It doesn’t seem to matter whether kwargs is a Dict{Symbol, Int64} or a Tuple{Pair{Symbol, Int64}}.

It’s documented:

The nature of keyword arguments makes it possible to specify the same argument more than once. For example, in the call plot(x, y; options..., width=2) it is possible that the options structure also contains a value for width . In such a case the rightmost occurrence takes precedence; in this example, width is certain to have the value 2 . However, explicitly specifying the same keyword argument multiple times, for example plot(x, y, width=2, width=3) , is not allowed and results in a syntax error.

3 Likes