`pop!` doesn't work with `Iterator.Pairs`

I was surprised by this:

function test(;args...)
    for k in keys(args)
        pop!(args,k,nothing)
    end
end

When called as test(z=1), this error results:

ERROR: MethodError: no method matching pop!(::Base.Iterators.Pairs{Symbol,Int64,Tuple{Symbol},NamedTuple{(:z,),Tuple{Int64}}}, ::Symbol, ::Nothing)

Since the type of args is Base.Iterators.Pairs <: AbstractDict, I expected pop! to work. Other “dictionary-type” functions such as keys work fine. Is this missing functionality, or is my expectation wrong?

Is there a function that provides similar functionality to pop! for key-value iterators over named tuples?

Found a solution:

function test(;args...)
    d = Dict(args)
    for k in keys(d)
        pop!(d, k, nothing)
    end
end

I think your expectations are reasonable; please open an issue on Github.

I’ve opened an issue

2 Likes