-
Notifications
You must be signed in to change notification settings - Fork 28
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
base: master
Are you sure you want to change the base?
Conversation
Codecov Report
@@ 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
Continue to review full report at Codecov.
|
@@ -48,11 +48,11 @@ end | |||
|
|||
## getproperty is equivalent to indexing with a `Symbol` | |||
|
There was a problem hiding this comment.
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 | ||
|
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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] |
There was a problem hiding this comment.
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
.
Bump @andyferris :) |
# d[s] = x | ||
# return x | ||
# end | ||
@propagate_inbounds Base.getproperty(d::AbstractDictionary, s::Symbol) = d[s] |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So something like
@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))") |
Seconding the bump on this, a Dictionary with |
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.