Skip to content

Commit

Permalink
More flexible get, e.g. get(dict, key, nothing)
Browse files Browse the repository at this point in the history
  • Loading branch information
andyferris committed Mar 5, 2021
1 parent aeab543 commit e4d8e02
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
authors = ["Andy Ferris <[email protected]>"]
name = "Dictionaries"
uuid = "85a47980-9c8c-11e8-2b9f-f7ca1fa99fb4"
version = "0.3.7"
version = "0.3.8"

[deps]
Indexing = "313cdc1a-70c2-5d6a-ae34-0150d3930a38"
Expand Down
8 changes: 2 additions & 6 deletions src/indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@ end
get(d, convert(I, i), default)
end

@propagate_inbounds function Base.get(d::AbstractDictionary{I, T}, i::I, default) where {I, T}
get(d, i, convert(T, default))
end

@propagate_inbounds function Base.get(d::AbstractDictionary{I, T}, i::I, default::T) where {I, T}
@propagate_inbounds function Base.get(d::AbstractDictionary{I}, i::I, default) where {I}
(hasindex, t) = gettoken(d, i)
if hasindex
return gettokenvalue(d, t)
Expand All @@ -42,7 +38,7 @@ end
if hasindex
return gettokenvalue(d, t)
else
return convert(T, f())
return f()
end
end

Expand Down
4 changes: 2 additions & 2 deletions src/insertion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -254,12 +254,12 @@ function Base.get!(f::Callable, d::AbstractDictionary{I}, i) where {I}
end


function Base.get!(f::Callable, d::AbstractDictionary{I}, i::I) where {I}
function Base.get!(f::Callable, d::AbstractDictionary{I, T}, i::I) where {I, T}
(hadindex, token) = gettoken!(d, i)
if hadindex
return @inbounds gettokenvalue(d, token)
else
default = f()
default = convert(T, f())
@inbounds settokenvalue!(d, token, default)
return default
end
Expand Down

2 comments on commit e4d8e02

@andyferris
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/31339

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.3.8 -m "<description of version>" e4d8e0297f3942dc06f5f57d3313dea23c0f6ff4
git push origin v0.3.8

Please sign in to comment.