-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Add sugar v[a .. b] be sugar for vec.slice and str.slice #4160
Comments
What about having some sort of sugar for
Also (from this article):
D also allows you to overload the slice and slice assignment operators. The nice thing about being able to overload those operators would be that you'd be able to implement vector slicing in core, as opposed to it being some compiler magic. |
How about copying the python syntax (http://stackoverflow.com/questions/509211/good-primer-for-python-slice-notation). I.e (in python):
|
That's a decent idea. So |
|
Oh, silly me. Yeah I see. That seems like an elegant solution. |
+1 for copying the Python syntax. |
+1 on the Python syntax (including negative indices to specify offsets from the end of the vector). It would also be nice to allow the operator to be overloaded (e.g., |
Special interpretation of negative numbers scares me. It sounds both slow (requiring a branch) and dangerous (you expected a crash, but instead you got the last element of the array). I prefer D's approach. |
I also prefer the D syntax using |
The issue with using |
Does |
For the rare case that we would need
Here |
I also favor the python syntax, it comes very handy. Including using negative numbres to specify the offset from the end of an array. Does it really need a branch? |
Prefer not to add $ or ^ support, nor a syntax different from ".." for ranges (which is currently supported in expression and pattern grammars, and may be worth using in type grammar rather than the current "* N"). 0 and foo.len() are ok for the explicit ends. Think simplicity and symmetry. Keep in mind we now support restructuring vector patterns, which soaks up some of this feature. |
I don't like using |
I don't think this is backwards incompatible. |
(Also I vote delaying this to Rust 2.0) |
Nominating for "not backwards incompatible". |
The syntax should probably be |
yeah I think this is a feature or a far-future, not b-c. |
accepted for far future milestone |
This is a good and relatively uncontroversial project for a new compiler hacker. |
Wouldn't the |
@glehel: I think it might. That's why I still like the |
I fully endorse this syntaxic enhancement. I'm quite disappointed it would be delayed to "a far futur" however... |
Just for the record, I favor this strongly, and this is my preferred form.
Note there is no special syntax for length. That eliminates some use cases. Oh well. There is also no accommodation for negative indices. Oh well. These things are not that important: the main use is
|
I like @nikomatsakis proposal. However, I think negative indices should be used like in Ruby, -1 would be the last element, so [-5..-1] would give the last 5 elements. |
cc me I like this proposal! |
Hm, most of the time I am either dropping off elements from the front of a vector or from the tail of it, or more seldom from both sides at the same time. Without using negative indices, the slice operator is missing a common use-case (dropping off elements from the end). BUT, I don't like the use of negative numbers for counting backwards. Something like So my suggestion would be to not include the slice operator at all, but provide more understandable methods like |
Extending indexing from elements |
+1 to nmatsakis' proposal verbatim. No comma, no syntax for length, no negative indices. Clean and simple. |
Another +1 to nikomatsakis' proposal. |
Another +1 for Niko's proposal. It's well thought-out. |
The one problem with @nikomatsakis's proposal is it requires contiguous storage, so e.g. doesn't work with rope types, or vectors with strides. |
One more +1 for Niko's proposal. |
I don't think the complexity is worth it really. |
+1
|
Clearly this is RFC material. I'm going to close this issue and open one on the rfcs repo :) |
** UPDATE by @nikomatsakis **
Closed in favor of this RFC repo issue: rust-lang/rfcs#13
** ORIGINAL **
@graydon said in irc today:
The text was updated successfully, but these errors were encountered: