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

Lack of NamedTuple indexing methods #27021

Open
davidagold opened this issue May 7, 2018 · 6 comments
Open

Lack of NamedTuple indexing methods #27021

davidagold opened this issue May 7, 2018 · 6 comments
Labels
domain:collections Data structures holding multiple items, e.g. sets

Comments

@davidagold
Copy link
Contributor

Couldn't find an existing issue that quite addressed this, apologize if dup (or naïve).

julia> versioninfo()
Julia Version 0.7.0-DEV.5008
Commit 2cab814429 (2018-05-05 14:43 UTC)
Platform Info:
  OS: macOS (x86_64-apple-darwin17.5.0)
  CPU: Intel(R) Core(TM) i5-4278U CPU @ 2.60GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-3.9.1 (ORCJIT, haswell)
Environment:
  JULIA_REVISE = manual

julia> x = (1, 2); y = (a = 1, b = 2);

julia> x[1:2]
(1, 2)

julia> y[1]
1

julia> y[2]
2

julia> y[1:2]
ERROR: MethodError: no method matching getindex(::NamedTuple{(:a, :b),Tuple{Int64,Int64}}, ::UnitRange{Int64})
Closest candidates are:
  getindex(::NamedTuple, ::Int64) at namedtuple.jl:100
  getindex(::NamedTuple, ::Symbol) at namedtuple.jl:101

julia> y[[:a, :b]]
ERROR: MethodError: no method matching getindex(::NamedTuple{(:a, :b),Tuple{Int64,Int64}}, ::Array{Symbol,1})
Closest candidates are:
  getindex(::NamedTuple, ::Int64) at namedtuple.jl:100
  getindex(::NamedTuple, ::Symbol) at namedtuple.jl:101

It just might be nice to have these indexing methods at some point down the road.

Thoughts?

@ararslan ararslan added the domain:collections Data structures holding multiple items, e.g. sets label May 7, 2018
@bramtayl
Copy link
Contributor

bramtayl commented May 8, 2018

Worth noting that indexing a named tuple with arrays will be type unstable. What would be nice is to be able to index them with tuples:

y[(1, 2)] and y[(:a, :b)].

Getting constants to propagate correctly for type stability would be a challenge.

@eulerkochy
Copy link
Contributor

Worth implementing, should I go forward with this?

@andyferris
Copy link
Member

I should xref #24019 - I feel we should have a full set of non-scalar indexing operations between any AbstractArray, AbstractDict, Tuple and NamedTuples.

@andyferris
Copy link
Member

Worth noting that indexing a named tuple with arrays will be type unstable.

@bramtayl If the output is an array and we preserve the eltype, it should be type stable (consider also the special case where all elements of the named tuple have the same type).

@andyferris
Copy link
Member

I'll also repeat something I wrote on the PR:

Finally, due to some ambiguities in dictionaries (and setindex!) I believe there was some plan to have a "non-scalar indexing" notation or operation or something - perhaps @mbauman knows what the plan is these days. (Matt - I kind-of assumed any new non-scalar indexing might end up there, is that right?)

So I think this is discussed in #30845

@felixcremer
Copy link
Contributor

|

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain:collections Data structures holding multiple items, e.g. sets
Projects
None yet
Development

No branches or pull requests

6 participants