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

Tuple and NamedTuple indexing corner cases #29417

Open
bkamins opened this issue Sep 28, 2018 · 3 comments
Open

Tuple and NamedTuple indexing corner cases #29417

bkamins opened this issue Sep 28, 2018 · 3 comments

Comments

@bkamins
Copy link
Member

bkamins commented Sep 28, 2018

I want to raise some cases with indexing Tuple and NamedTuple.

Tuple

You are allowed to index Tuple with values that are not allowed for arrays:

julia> (1,2,3)[1.0]
1

julia> (1,2,3)[true]
1

Should we disallow this?

NamedTuple

You are not allowed to index NamedTuple with vectors, e.g.

julia> (a=1,b=2,c=3)[[1,2]]
ERROR: MethodError: no method matching getindex(::NamedTuple{(:a, :b, :c),Tuple{Int64,Int64,Int64}}, ::Array{Int64,1})
Closest candidates are:
  getindex(::NamedTuple, ::Int64) at namedtuple.jl:101
  getindex(::NamedTuple, ::Symbol) at namedtuple.jl:102
Stacktrace:
 [1] top-level scope at none:0

julia> (a=1,b=2,c=3)[[:a,:b]]
ERROR: MethodError: no method matching getindex(::NamedTuple{(:a, :b, :c),Tuple{Int64,Int64,Int64}}, ::Array{Symbol,1})
Closest candidates are:
  getindex(::NamedTuple, ::Int64) at namedtuple.jl:101
  getindex(::NamedTuple, ::Symbol) at namedtuple.jl:102
Stacktrace:
 [1] top-level scope at none:0

julia> (a=1,b=2,c=3)[[true,false,true]]
ERROR: MethodError: no method matching getindex(::NamedTuple{(:a, :b, :c),Tuple{Int64,Int64,Int64}}, ::Array{Bool,1})
Closest candidates are:
  getindex(::NamedTuple, ::Int64) at namedtuple.jl:101
  getindex(::NamedTuple, ::Symbol) at namedtuple.jl:102
Stacktrace:
 [1] top-level scope at none:0

Here the question is if we should allow this.

CC @nalimilan, @pdeffebach

@nalimilan
Copy link
Member

I think we should support the same operations for NamedTuple as for Tuple. I don't see why we would be more restrictive for the latter, except maybe for things like nt[true] and nt[1.0] which don't work with arrays and should probably never have been allowed for tuples.

@bkamins
Copy link
Member Author

bkamins commented Oct 11, 2018

This is what I mean. The question is if nt[true] and nt[1.0] is a bug or a feature 😄.

@mcabbott
Copy link
Contributor

mcabbott commented Jan 6, 2021

xref issues #27021, #38834, and PRs #31423, #38878

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

No branches or pull requests

3 participants