-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Add getindex methods for NamedTuple #38878
Conversation
More stupid typos 🤦 - got it fixed locally but will wait to push so as not to trigger extra CI time |
Bump! Was wondering if maybe failing CI was keeping people from reviewing so I pushed the typo fix. |
@@ -116,6 +119,8 @@ firstindex(t::NamedTuple) = 1 | |||
lastindex(t::NamedTuple) = nfields(t) | |||
getindex(t::NamedTuple, i::Int) = getfield(t, i) | |||
getindex(t::NamedTuple, i::Symbol) = getfield(t, i) | |||
@inline getindex(t::NamedTuple, idxs::Tuple{Vararg{Symbol}}) = NamedTuple{idxs}(t) |
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.
Sorry if this has been discussed elsewhere, but why the @inline
? Seems to not match the other methods around and should be simple enough inline by itself?
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.
That's how Jeff wrote it in the attached issue (#38834) - TBH I still don't totally understand what @inline
does 🤷 . Happy to leave or remove
Happy New Year! Bump :-P |
Alright, removed comments from tests, and also rebased onto current master branch. Open questions:
|
Yeah, add a compat notice |
Probably also worth adding an entry in NEWS.md |
See #38834
This adds
getindex()
methods forNamedTuple
s to return multiple values as a newNamedTuple
, eg:To be analogous with
getindex
forTuple
s andVector
s. When Jeff commented on the issue, he only gave explicit approval to the idea of using a tuple ofSymbol
s to index, not theAbstractVector
, but it's easy enough to remove the latter if unwanted. If theAbstractVector
method is approved, in the tests, I wasn't sure if testing all of the methods in 1 test would be a good idea or not (see comments) - I assumed keeping them separate would be better.I also added a bit to the docstring, but I wasn't sure if I should also add something to
base/docs/basedocs.jl
somewhere around hereOpen questions / todos
getindex(t::NamedTuple, idxs::AbstractVector{Symbol})
?basedocs.jl
?If there's anything else I've missed, please let me know!