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

RFC: Change syntax of subslice matching #202

Merged
merged 2 commits into from
Sep 3, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions active/0000-subslice-syntax-change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
- Start Date: 2014-08-15
- RFC PR:
- Rust Issue:

# Summary

Change syntax of subslices matching from `..xs` to `xs..`
to be more consistent with the rest of the language
and allow future backwards compatible improvements.

Small example:

```rust
match slice {
[xs.., _] => xs,
[] => fail!()
}
```

This is basically heavily stripped version of [RFC 101](https://github.com/rust-lang/rfcs/pull/101).

# Motivation

In Rust, symbol after `..` token usually describes number of things,
as in `[T, ..N]` type or in `[e, ..N]` expression.
But in following pattern: `[_, ..xs]`, `xs` doesn't describe any number,
but the whole subslice.

I propose to move dots to the right for several reasons (including one mentioned above):

1. Looks more natural (but that might be subjective).
2. Consistent with the rest of the language.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What in particular is it consistent with?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I shouldn't say that it is consistent, but rather that it is not inconsistent. Current syntax suggest that when matching with ..x, x would be bound to a count of elements, but it is somewhat surprisingly bound to whole slice. In my previous RFC I proposed that x in this case should really mean a number, and that would require moving xs to the left of ... This proposal alone makes less sense, because it somewhat cut down version, but still I think proposed behaviour is less surprising than current and provides ability to make future backwards compatible changes (which would allow consistent behaviour of token after .. meaning a number).

Unfortunatelly, as @glaebhoerl noticed, I didn't think about syntax of struct update at all.

3. C++ uses `args...` in variadic templates.
4. It allows extending slice pattern matching as described in [RFC 101](https://github.com/rust-lang/rfcs/pull/101).

# Detailed design

Slice matching grammar would change to (assuming trailing commas;
grammar syntax as in Rust manual):

slice_pattern : "[" [[pattern | subslice_pattern] ","]* "]" ;
subslice_pattern : ["mut"? ident]? ".." ["@" slice_pattern]? ;

To compare, currently it looks like:

slice_pattern : "[" [[pattern | subslice_pattern] ","]* "]" ;
subslice_pattern : ".." ["mut"? ident ["@" slice_pattern]?]? ;

# Drawbacks

Backward incompatible.

# Alternatives

Don't do it at all.

# Unresolved questions

Whether subslice matching combined with `@` should be written as `xs.. @[1, 2]`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you clarify how it is written at present and with this proposal?

or maybe in another way: `xs @[1, 2]..`.