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: Alternatives in patterns #1500

Closed
wants to merge 1 commit into from
Closed
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
120 changes: 120 additions & 0 deletions text/0000-alternatives-in-patterns.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
- Feature Name: Alternatives in patterns
- Start Date: 2016-02-14
- RFC PR: (leave this empty)
- Rust Issue: (leave this empty)

# Summary
[summary]: #summary
Extend the pattern syntax for alternatives in `match` statement, allow alternatives for pattern matching in `let` and `if let` statements.

# Motivation
[motivation]: #motivation

Rust allows alternatives ( `|` ) in pattern syntax for `match`, but only for 'top-level' of pattern.
This aims to reduce verbosity in certain examples and increase expressiveness.

Also, this RFC proposes to allow alternatives in `let` or `if let` statements.

# Detailed design
[design]: #detailed-design

## Expand usage of alternatives to 'deeper levels'
Imagine a type:
```rust
struct NewType (Result<String,String>, u8);
```

Exhaustive `match` statement for this type would look like this:
```rust
match new_type {
NewType(Ok(e), num) | NewType(Err(e), num) => println!("ok with {}: {}", num, e)
}
```

This RFC proposes a following replacement:
```rust
match new_type {
NewType(Ok(e) | Err(e), num) => println!("ok with {}: {}", num, e)
}
```

Choose a reason for hiding this comment

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

This only works if for Result<T, T> , when Ok and Err both has same value type. I don't think this is good use case.

Copy link
Author

Choose a reason for hiding this comment

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

Well, that's an example, but for generalicity's sake can be replaced with Ok(_) | Err(_)

Choose a reason for hiding this comment

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

No, it can't. Because e is used in arm. If you don't want to use, then it may be written as NewType(_, num)

Copy link
Author

Choose a reason for hiding this comment

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

Ah, right. But I've used Result not to introduce a new enum.



Little bit more complicated example:
```rust
enum Test { First, Second }
//Current Rust:
match (test1, test2) {
(First, First) | (First, Second) | (Second, First) | (Second, Second) => println!("matches")
}

//This RFC proposes:
match (test1, test2) {
(First| Second, First | Second) => println!("matches")
}
```

## Allow alternatives in `let` statements
Currently expressions like `Ok(e) | Err(e)` are not allowed in `let` or `if let` statements, which brings inconsistence to pattern matching.
This RFC proposes following to be allowed:
```rust
if let (First | Second) = three_variants_enum {}
let (Ok(e) | Err(e)) = result;
let closure = |(Ok(e) | Err(e))| println!(e); // works similar to the statement above
```

### Parentheses around patterns in `let` statements
Multiple alternatives should be enclosed in parentheses and represent a single pattern,
while single alternative should not be enclosed with parens to be backwards compatible.
```rust
enum Three{ A(i32), B(i32), C(i32) }
if let A(i) = three {}
if let (A(i) | B(i)) = three {}
```
Parens should be introduced due to:
- Pattern matching in closure arguments:

```rust
let closure = | Ok(i) | Err(i) | i; // Is it possible to find pattern's end and actual closure' start?

let closure = |(Ok(i) | Err(i))| i; // As proposed by this RFC
```
- Follow rules for macros (as discussed in [1384#comment](https://github.com/rust-lang/rfcs/pull/1384#issuecomment-164275799))

### Irrefutable patterns
Patterns in `let` statements must be irrefutable - meaning they must cover every possible variant:
```rust
enum Three{ First(u8), Second(u8), Third(u8) }
//...
let (First(u) | Second(u)) = three; //Not allowed!
```

Patterns in `if let` statements should be disallowed to be irrefutable, it means they are not allowed to cover every possible variant:
```rust
if let (Ok(e) | Err(e)) = result {
//Not allowed!
} else {}
```
If pattern is irrefutable, then an `else`-branch will never be executed, and `if` will be redundant.

# Drawbacks
[drawbacks]: #drawbacks

These features, probably, are not easy to implement.

# Alternatives
[alternatives]: #alternatives

- **This is a subset of [#99](https://github.com/rust-lang/rfcs/pull/99).** The original RFC was postponed and as suggested by [#1456](https://github.com/rust-lang/rfcs/issues/1456#issuecomment-173943563) a new RFC was created with a link to postponed one.
- **Implement the proposal only for `match`.** This has a downside of further increased inconsistence.
- **Allow irrefutable patterns in `if let` statements.** This way, `else`-branch will not always execute. If so, a warning about unreachable code should be emitted.

# Unresolved questions
[unresolved]: #unresolved-questions

- The possibility of treating single variant w/o parens as a pattern (as it is treated today) simultaneously with treating multiple variants with parens as a pattern.
- The requirement of parens around multiple alternatives in *deeper levels* of pattern matching, i.e is this legal:
```rust
match new_type {
NewType(Ok(e) | Err(e), num) => println!("ok with {}: {}", num, e)
}
```