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

Using end with keyword indexing #146

Open
mcabbott opened this issue Nov 4, 2020 · 3 comments
Open

Using end with keyword indexing #146

mcabbott opened this issue Nov 4, 2020 · 3 comments

Comments

@mcabbott
Copy link
Collaborator

mcabbott commented Nov 4, 2020

Maybe this needs an issue. With keyword indexing, end still seems to be replaced as if being used with positional indexing. This can lead to wrong answers or to errors:

julia> nda = NamedDimsArray{(:a, :b)}(reshape(1:12, 3,4))
3×4 NamedDimsArray{(:a, :b), Int64, 2, Base.ReshapedArray{Int64, 2, UnitRange{Int64}, Tuple{}}}:
 1  4  7  10
 2  5  8  11
 3  6  9  12

julia> nda[b=end, a=1]
7

julia> nda[b=3, a=1]
7

julia> nda[b=4, a=1]
10

julia> nda[b=1, a=end]
ERROR: BoundsError: attempt to access 3×4 reshape(::UnitRange{Int64}, 3, 4) with eltype Int64 at index [4, 1]
Stacktrace:

julia> nda[a=end]
ERROR: BoundsError: attempt to access 3×4 reshape(::UnitRange{Int64}, 3, 4) with eltype Int64 at index [12, 1:4]

I don't think there's anything this package can do about this. Apart from adding warnings in the manual.

But possibly Base ought not to allow this at all. Or ideally to lower it to Base.lastindex(x, :a) using the name of the keyword.

@timholy
Copy link

timholy commented Nov 4, 2020

Worth someone testing whether https://github.com/JuliaArrays/EndpointRanges.jl works better.

@rafaqz
Copy link

rafaqz commented Nov 5, 2020

Base.lastindex(x, :a) is perfect.

@mcabbott
Copy link
Collaborator Author

mcabbott commented Nov 5, 2020

Here is the relevant bit of the parser, does not look very easy to adjust:

https://github.com/JuliaLang/julia/blob/280b9eafbfe1891d2779deb506ba4f9a2fff38d9/src/julia-syntax.scm#L89-L157

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