Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LittleDict / OrderedDict pop! consistency #61

Open
mattBrzezinski opened this issue Oct 6, 2020 · 1 comment
Open

LittleDict / OrderedDict pop! consistency #61

mattBrzezinski opened this issue Oct 6, 2020 · 1 comment

Comments

@mattBrzezinski
Copy link
Member

@iamed2 pointed this out in #59, here. We do not have consistency between the pop! function for LittleDict and OrderedDict.

LittleDict

Will return nothing if the key is not found.

function Base.pop!(dd::UnfrozenLittleDict, key)
@assert length(dd.keys) == length(dd.vals)
for ii in 1:length(dd.keys)
cand = @inbounds dd.keys[ii]
if isequal(cand, key)
deleteat!(dd.keys, ii)
val = @inbounds dd.vals[ii]
deleteat!(dd.vals, ii)
return val
end
end
end

OrderedDict

Will throw a KeyError if the key is not found.

function pop!(h::OrderedDict, key)
index = ht_keyindex(h, key, false)
index > 0 ? _pop!(h, index) : throw(KeyError(key))
end

I propose that we make the following changes:

  1. Have LittleDict throw a KeyError when the key is not found.
  2. Introduce a new function which provides a default value to return if key DNE (LittleDict pop! default #59)
@iamed2
Copy link
Contributor

iamed2 commented Oct 6, 2020

In support:

julia> pop!(Dict(), "baz")
ERROR: KeyError: key "baz" not found
Stacktrace:
 [1] pop!(::Dict{Any,Any}, ::String) at ./dict.jl:570
 [2] top-level scope at REPL[3]:1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants