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

Allow multiple (fixed-size) subslices borrows in one pattern #101

Closed
wants to merge 2 commits into from

Conversation

krdln
Copy link
Contributor

@krdln krdln commented Jun 1, 2014

Change syntax in slice pattern matching from ..xs to xs.. (eg. [head, tail..]),
and allow multiple (fixed-size) subslices borrows in one pattern (eg. [xs..3, ys..3]).

@glaebhoerl
Copy link
Contributor

+1, I was going to propose the same thing. If we allow runtime values for the lengths of the subslices, and add support for multiple mut ref borrows inside an array pattern (which we should do anyway), then our language support for safely manipulating mutable arrays will be completely first-class, and current unsafely implemented functions like mut_split_at will become redundant.

(I'm not entirely sold on the syntax, but I think your idea is better than anything I came up with.)

@emberian
Copy link
Member

cc @jakub-

@ghost
Copy link

ghost commented Jun 27, 2014

-1. I think I'd actually be in favour of removing them altogether or reducing them to only the simplest cases ([a, b, c, ..tail]).

@ghost
Copy link

ghost commented Jun 27, 2014

To elaborate, they're making the pattern sanity check pass much more complicated than it really needs to be and add significant disconnect between that pass and the codegen for patterns. I think I'd also agree they also don't fit well with the other patterns that only cover ADTs, which vectors are not.

@zwarich
Copy link

zwarich commented Jun 28, 2014

Is it even possible to check exhaustiveness and redundancy of these kinds of patterns?

@glaebhoerl
Copy link
Contributor

I think it would have to be conservative, i.e. if the compiler can't prove exhaustiveness, require a wildcard pattern.

@krdln
Copy link
Contributor Author

krdln commented Jun 28, 2014

@zwarich
I don't see why checking exhaustiveness would be problematic in these kind of patterns. Remember that they are all fixed-sized, so they cannot match more than something like [_,_,_], which is allowed today.

@zwarich
Copy link

zwarich commented Jun 28, 2014

Sorry, I totally missed the fixed-size qualification.

@glaebhoerl
Copy link
Contributor

It depends on whether you allow the fixed sizes to be runtime values. If you do, my answer applies. If you don't, @krdln's does. I think.

Edit: Hm, I see now that runtime values for the fixed sizes was never mentioned in the OP. Mea culpa. I misinterpreted the unresolved question section.

@glaebhoerl
Copy link
Contributor

@nick29581

#101 - Allow multiple (fixed-size) subslices borrows in one pattern - krdln
Allows matching several parts of a vec in one pattern. Adds xs..n syntax to match a fixed size slice (and changes variable sized slice pattern syntax to xs.. from ..xs).
Not much feedback - all positive or corrected later to be positive. Seems like a small generalisation with no downsides.
If we change the syntax as recommended for existing patterns (i.e., ..xs to xs..) then the rest should be backwards compatible, I think.

FWIW, I think this syntax change makes some logical sense on its own terms.

We currently use the syntax ..FOO in a number of places to mean various vaguely similar but different things. The idea here would be to add a bit more distinction, so that FOO.. means "these things", for instance in slice patterns and functional struct updates, while ..FOO means "this many things", e.g. in fixed-length arrays and element-replication for same.

And then as a side benefit, it becomes possible to have both of them where the cases overlap, as with the fixed-length slice patterns in this proposal.

@rkjnsn
Copy link
Contributor

rkjnsn commented Aug 5, 2014

+1 I like this. At the very least, I think we should change ..xs to xs.. as I find it clearer and more logical, and it would allow the rest to be implemented at a later date if it isn't accepted, now.

@nrc
Copy link
Member

nrc commented Aug 15, 2014

This was discussed at this week's Rust meeting (https://github.com/rust-lang/meeting-minutes/blob/master/weekly-meetings/2014-08-12.md). We decided that we don't want to implement these changes in full, in particular that enhancing slice matching is something we are a little bit uncomfortable with and needs some thought and planning but is low enough priority that we don't want to do that before 1.0.

However, we are sympathetic to leaving this option open in the future, so we would like to make the change to slice syntax (the position of ..). @krdln would you like to submit a new, cut down version of this RFC with just the syntactic changes required to make the rest of this RFC backwards compatible? If you would prefer not to, then I'm happy to do it too, just let me know.

Thanks for the RFC!

@krdln
Copy link
Contributor Author

krdln commented Aug 16, 2014

I'm happy to hear that there's a slight possibility that my RFC might be accepted in a far future!
And I will be happy to write new RFC consisting of only .. change. Will try to do it today.

Edit: Here it is: #202

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

Successfully merging this pull request may close these issues.

6 participants