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

Enable getproperty for Symbol keys #43

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

andyferris
Copy link
Owner

This enables convenient access to dictionaries with Symbol keys. One potential use case is the rows or columns of a table, similar to a dataframe.

@codecov
Copy link

codecov bot commented Feb 6, 2021

Codecov Report

Merging #43 (80a2f3b) into master (aeab543) will decrease coverage by 0.16%.
The diff coverage is 33.33%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #43      +/-   ##
==========================================
- Coverage   70.34%   70.18%   -0.17%     
==========================================
  Files          19       19              
  Lines        1703     1707       +4     
==========================================
  Hits         1198     1198              
- Misses        505      509       +4     
Impacted Files Coverage Δ
src/indexing.jl 53.84% <0.00%> (-3.54%) ⬇️
src/Indices.jl 81.22% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update aeab543...80a2f3b. Read the comment docs.

@@ -48,11 +48,11 @@ end

## getproperty is equivalent to indexing with a `Symbol`

Copy link
Contributor

@aplavin aplavin Mar 2, 2023

Choose a reason for hiding this comment

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

Base.propertynames(d::AbstractDictionary) = keys(d)

@propagate_inbounds function Base.setproperty!(d::AbstractDictionary, s::Symbol, x)
d[s] = x
return x
end

Copy link
Contributor

Choose a reason for hiding this comment

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

Also

using ConstructionBase
ConstructionBase.setproperties(obj::AbstractDictionary, patch::NamedTuple) = merge(obj, Dictionary(keys(patch), values(patch)))

would be nice

Copy link
Owner Author

Choose a reason for hiding this comment

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

With Julia 1.9 it'll be a lot easier to include opt-in "weak" dependencies on other packages on the ecosystem.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, that was just a suggestion - ConstructionBase is so lightweight and widely used that I didn't even think it can/should be made a weakdep.
I may make a PR with ConstructionBase and Accessors integrations then.

# d[s] = x
# return x
# end
@propagate_inbounds Base.getproperty(d::AbstractDictionary, s::Symbol) = d[s]
Copy link
Contributor

Choose a reason for hiding this comment

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

Strings and Ints also work: d."abc" and d.:123.

@aplavin
Copy link
Contributor

aplavin commented Mar 2, 2023

Bump @andyferris :)

# d[s] = x
# return x
# end
@propagate_inbounds Base.getproperty(d::AbstractDictionary, s::Symbol) = d[s]
Copy link
Collaborator

Choose a reason for hiding this comment

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

One could probably restrict the AbstractDictionary type to ones containing Symbol keys only and return an explicit error otherwise.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also Strings and Ints should work, as I commented half a year ago above :)

Copy link
Collaborator

Choose a reason for hiding this comment

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

So something like

Suggested change
@propagate_inbounds Base.getproperty(d::AbstractDictionary, s::Symbol) = d[s]
@propagate_inbounds Base.getproperty(d::AbstractDictionary{T}, s::T) where {T<:Union{Symbol,Integer,String}} = d[s]
Base.getproperty(d::AbstractDictionary{T}, s) where {T} = error("`getproperty` can only be used on dictionaries with keys of type `Symbol`, `Integer` or `String` (got $(T))")

@rafaqz
Copy link

rafaqz commented Dec 12, 2023

Seconding the bump on this, a Dictionary with getproperty on keys would be perfect for seamless switching between Dictionary and NamedTuple for runtime/compilation tradeoffs, e.g. in DimensionalData.jl DimStack.

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

Successfully merging this pull request may close these issues.

4 participants