-
-
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
RFC: start(arr) vs. 1 for bounds checking, iteration vs. indexing #11713
Comments
Shouldn't it be |
So, is there a need for another verb here? One motivating case would be Fortran-like offset arrays, which are more possible these days without colon lowering to |
Just getting the first index isn't good enough for an offset array. You need the first index in a given dimension. Not only is the lower bound wrong, the upper bound ( |
Slightly tangential but couldn't |
No,
|
Ufff... looks like I've opened another can of worms! |
No. Use |
Thanks! So more work would be needed in Base for offset arrays then before one could attempt such a package? Could you think of any more places work would be needed offhand? |
@mbauman The code has: startpos < start(dat) && throw(BoundsError(dat, startpos))
(startpos <= endpos <= endof(dat)) || throw(BoundsError(dat, endpos))
|
Ah, yes, that makes sense. You could use |
@ScottPJones Your second line doesn't look completely correct actually. If one passes |
@nalimilan OK, but will |
Indexing and iteration are separate concepts. Whatever gets returned by |
I was using |
Yes, this is fairly subtle and the split only happened recently in #10704. It definitely needs more documentation, and I'll put it on my list for my planned Interfaces manual page. Looking over at #11575, you're not working with general |
|
And to correctly check both a start and end position for bounds of a vector, what would be the best way? |
startpos < first(eachindex(dat)) && throw(BoundsError(dat, startpos))
checkbounds(dat, endpos)
endpos < startpos && throw(ArgumentError("End position ($endpos) is less than start position ($startpos)")) would that be good? |
Depends on what kind of error messages you want, and what kind of vector you're working with. I'd probably use The error reports indexing with a range, though, so there's a tradeoff for the simplicity. Personally, I think it's fine. |
OK, thanks! |
@mbauman As you say, the error message is not that great with your proposal. Wouldn't calling |
I had thought that Cc:@timholy |
@nalimilan You're right, the emitted LLVM is very similar. The difference is likely negligible:
|
@mbauman When is your Interfaces manual planned for? It sounds like that would be a wonderful and much needed addition to the documentation! (I realize everybody is quite busy, and documentation is often the last thing somebody wants to do...) |
It's currently planned to get done on the day it gets done. :) Definitely aiming for 0.4. |
The discussion here seems best continued on other related AbstractArray / indexing issues/PR's slated for 0.5. |
In the review of my PR #11575, @tkelman requested I change a 1 to
start(dat)
in the bounds checking.I did find a case where
start()
on anAbstractArray
returns a tuple, instead of 1.I also found numerous cases, just in Base (I'm sure there are many more in the registered Packages),
where 1 was used instead of
start()
for bounds checking.@nalimilan and @mbauman both responded, with some good information, and some ideas for what might need to be done to resolve these issues.
What really needs to be done here?
The text was updated successfully, but these errors were encountered: