Skip to content

Allow an optional vert at the beginning of a match #36361

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
mdinger opened this issue Sep 9, 2016 · 4 comments
Closed

Allow an optional vert at the beginning of a match #36361

mdinger opened this issue Sep 9, 2016 · 4 comments

Comments

@mdinger
Copy link
Contributor

mdinger commented Sep 9, 2016

See how in the following, all the | bars are mostly aligned except the last one:

#![allow(dead_code)]

use E::*;

enum E {
    A,
    B,
    C,
    D,
}

fn main() {
    match A {
        A |
        B |
        C |
        D => (),
    }
}

I'd propose it be allowed at the beginning of the pattern as well enabling something like this:

#![allow(dead_code)]

use E::*;

enum E {
    A,
    B,
    C,
    D,
}

fn main() {
    match A {
    | A
    | B
    | C
    | D => (),
    }
}

This appears to be the official style for F# matches and it has grown on me a lot. It highlights the matches and doesn't require as much deeper nesting. After getting used to the F# style, the inability to do this is rust seems a bit limiting.

I'm not sure if this issue should be in this repo or not but just direct me if I have to move it.

@KalitaAlexey
Copy link
Contributor

@mdinger Syntax addition requires a RFC. I like it more than:

#![allow(dead_code)]

use E::*;

enum E {
    A,
    B,
    C,
    D,
}

fn main() {
    match A {
    A
    | B
    | C
    | D => (),
    }

    // or

    match A {
      A
    | B
    | C
    | D => (),
    }
}

But I'm not sure about implementation's complexity.

@Victor-Savu
Copy link

I would like to point out two aspects (please correct me if I am wrong):

  • The example used to support this proposal should never appear in practice because there is only one branch which matches any of the members (i.e. the match expression is tautological and its result is equivalent to that of the single branch branch).
  • The proposed pattern can cause confusion in all but the (as far as I can tell) invalid case that was presented:
#![allow(dead_code)]

use E::*;

enum E {
    A,
    B,
    C,
    D,
}

fn main() {
    match A {
    | A
    | B => println!("Give me A | B!"),
    | C
    | D => println!("Why am I here?"),
    }
}

Please let me know if I misunderstood the proposal. Could you please provide an example which better illustrates the benefits of this syntax?

@KalitaAlexey
Copy link
Contributor

@Victor-Savu
You are wrong.

enum E { A, B, C }

fn main() {
    use E::*;
    let value = A;
    match value {
    A | B => {},
    C => {}
    }
}

After syntax changes could be rewritten like:

enum E { A, B, C }

fn main() {
    use E::*;
    let value = A;
    match value {
    | A
    | B => {},
    C => {}
    }
}

@mdinger
Copy link
Contributor Author

mdinger commented Sep 9, 2016

Please leave further comments in rust-lang/rfcs#1745

@mdinger mdinger closed this as completed Sep 9, 2016
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

No branches or pull requests

3 participants