-
Notifications
You must be signed in to change notification settings - Fork 331
Add .nth() method to Slice type #386
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
What meaning do we give to a step of 0? It used to be an error when using it for slicing, I'll have to check again.
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.
If you actually call
slice()
with a step of 0, then you'll get a panic. (Seedimension::do_slice
.) You'll also get a panic when callingslice()
if the slice begin/end is out-of-range.In contrast,
nth()
doesn't panic in these cases. It's possible to change it to panic ifstep == 0
, without changing the function signature. To make it panic for out-of-range indices, it would need an argument for the length of the axis you're going to slice.If you'd prefer it panic in those cases, I'd probably choose a different name (e.g.
index()
) instead ofnth()
.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.
Great, thanks for clarifying!
That sounds you have no particular behaviour in mind for 0, so we can choose one freely. I'm not too fond of mixing panics in explicitly checked or fallible methods either, so I'm not sure. I do lean towards that it should be a panic -- maybe a debug assertion. Maybe indeed a debug assertion already in Slice::new ? Or is that redundant?
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.
Ah with public fields a debug assertion in new doesn't do much good anyway.
Uh oh!
There was an error while loading. Please reload this page.
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.
It's certainly possible to make the fields non-public (or maybe just
step
) and add an assertion tonew()
andstep_by()
. In some ways, I think that's a better design anyway; what do you think? It's still not possible to handle out-of-range indices this way, but it would catchstep == 0
early.If we make some of the fields in
Slice
non-public, I would also changeenum SliceOrIndex { Slice { start, end, step }, ... }
toenum SliceOrIndex { Slice(Slice), ... }
to hide the fields there too.Edit: If it matters, Python's
slice
type does allowstep == 0
.