-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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 an optional vert at the beginning of a match branch #1925
Conversation
@mdinger One nit: in example B, why not include the vert to make all the branches match, rather than calling attention to the lack of one with a comment? More significantly, the "detailed design" section needs some details. In that section, you should discuss modifying the grammar for match arms to allow an optional single vertical bar |
Example B was to show it is still legal to be inconsistent in that way but
even in that case, it doesn't make the logic unclear.
I'll looked for the grammar but didn't see it. I'll have to look again
another day.
|
Actually, example B stemmed from a question on the original thread which I
think highlighted the possible confusion over something like that. So I
didn't think I should disregard it. Maybe it needs more comments
|
@mdinger In that case, you may want to show a "before" and "after" for example B: the inconsistent version, and the consistent version. |
I think for consistency it should also allow leading |
I'm finding it a bit hard to read the examples when I glance over them. The indenting makes it hard see where each pattern starts or ends, and to spot where each match occurs. So personally I don't think it would fit with the language unless it used a style like this:
|
I very much appreciated the ability to have leading That said, if we're going to accept leading |
@keeperofdakeys Allowing leading vert doesn't specify whether match arms should have a leading indent in addition to the @nikomatsakis @rust-lang-nursery/style, because for some reason we put fmt-rfcs under rust-lang-nursery. Also, for some reason that doesn't seem to actually highlight and work; do cross-organization team references not work? If they don't, all the more reason to consider moving the style team and fmt-rfcs ro rust-lang. Given the availability of leading |
To clarify, I never intended for this to be a proposal to change the recommended way matches are formatted. Stylistic topics are enormously difficult to push through to communities because everyone has their own preferences, which is okay and not problematic. My intent was to showcase that allowing the leading vert could be a very valuable addition to some people without hindering understanding for others. If you don't like it, fine, don't use it but that is no reason to hinder others if they like it. The best I probably hoped for was for the vert to be allowed and an example in the docs somewhere include the style so that people could decide if it is good. That is, I don't always wait for rust-lang.org to tell me if I like a coding style. Sometimes, I decide for myself (it's nice to have a menu to show me what my options are though - I'd never have discovered this style if I hadn't seen it in F#). @keeperofdakeys Styles are mutable. Your entire history in Rust and every other language colors your perception of what looks good. My history works the same way. I don't think I can categorically say that any simple style change of this sort could never look like Rust. Taking an example from regex: can you identify which lines are match arms in less than a second in the following example? match c {
'\\' => return match self.c() {
Some('#') => {self.advance(); Some('#')}
_ => Some('\\')
},
'#' => loop {
match self.c() {
Some(c) => {
self.advance();
if c == '\n' {
break;
}
},
None => return None
}
},
_ => if !c.is_whitespace() {return Some(c);}
} Repeating the example, if I told you every arm started with a vert, could you do it faster? I can. I think they highlight them: match c {
| '\\' => return match self.c() {
| Some('#') => {self.advance(); Some('#')}
| _ => Some('\\')
},
| '#' => loop {
match self.c() {
| Some(c) => {
self.advance();
if c == '\n' {
break;
}
},
| None => return None
}
},
_ => if !c.is_whitespace() {return Some(c);}
} |
I actually think this would be a good addition to the language, I just wanted to point out that I found the RFC examples a bit hard to read. People are free to use a feature how they want, but I think that RFC examples are a good indication of how a feature could/should be used. Ultimately a style rfc is the way to decide how it should be styled though. @mdinger I agree, it's easier to identify match arms in the second. |
When I have more variants than can fit on a line I write my matches like this: match foo {
Foo::Bar
| Foo::Baz
| Foo::Quux,
=> Whatever::One,
Foo::Spam
| Foo::Eggs,
=> Whatever::Two,
} I would probably write this after this RFC: match foo {
| Foo::Bar
| Foo::Baz
| Foo::Quux,
=> Whatever::One,
| Foo::Spam
| Foo::Eggs,
=> Whatever::Two,
} When I don't have a situation like this (which is most of the time), I make sure my matchrockets all align. Most people write their matches differently from me, but I would still like to have the grammar be more flexible. However, supporting |
Unfortunately the closing braces in the same column kills it for me right away, along with the more minor issue @keeperofdakeys pointed out where it becomes less clear which lines are the beginnings of patterns and which are not. |
@joshtriplett I added some updates to try to address your comments. To others regarding braces, considering how syntax heavy rust is, I actually find the extra braces just a slight nitpick and isn't even that bad. Compare with the fairly common match name
| Name Franks name =>
match name
| Franks Alice
| Franks Brenda
| Franks Dave => FavoriteBook
author: "alice berkley"
title: "Name of a popular book"
date: 1982
| Franks Charles
| Franks Steve => FavoriteBook
author: "fred marko"
title: "We'll use a different name here"
date: 1960
| Name Sawyer name =>
match name
| Sawyer Tom => FavoriteBook
author: "another name"
title: "Again we change it"
date: 1999
| Sawyer Sid
| Sawyer May => FavoriteBook
author: "again another name"
title: "here is a different title"
date: 1972 I'm not saying all the extra symbols need to go away but when you compare it to something like this which, for the most part, should be somewhat clear (and less heavy-handed), the debate about whether 1 or 2 different braces should be aligned isn't that important. The language is syntax heavy and we do the best we can. |
Um... I don't really like the argument "rust is already uglier than F#, so
it shouldn't matter if we make it a tiny bit more ugly here". I see the
advantages of this proposal, but I wonder if we can't find an indentation
style that doesn't make it harder to associate a given closing brace with
its opening.
…On Sat, Feb 25, 2017 at 11:18 PM, mdinger ***@***.***> wrote:
@joshtriplett <https://github.com/joshtriplett> I added some updates to
try to address your comments.
@keeperofdakeys <https://github.com/keeperofdakeys> I don't know how I
can make the examples more clear. Branches always occur where the =>
points. They always have and they always will. This appears to be finding
the syntax unfamiliar and thus *feels hard* (here's
<https://upload.wikimedia.org/wikipedia/en/b/b1/Backwards_text_refdesk.png>
something which technically should be really easy but probably will be
really hard). The only example I think is somewhat difficult to parse is
the first part of Example C. I find it tricky because the indentation is
always changing somewhat how the line below keeps moving. I find the second
part more consistent by far and much more straightforward.
[image: wavy]
<https://cloud.githubusercontent.com/assets/4156987/23336870/c0785aa6-fbaa-11e6-97d7-f17a3f75912c.png>
To others regarding braces, considering how syntax heavy rust is, I
actually find the extra braces just a slight nitpick and isn't even that
bad. Compare with the fairly common ))))) which pops up at the completion
of function calls. To the functional programmer, having tons of closing
parenthesis is probably even more dumb than an out of place brace
considering their languages don't even really require parenthesis. If
Example C was translated to F#, most of the symbols (,{}():: just
vaporize). Consider how clean this looks (not valid F#):
match name
| Name Franks name =>
match name
| Franks Alice
| Franks Brenda
| Franks Dave => FavoriteBook
author: "alice berkley"
title: "Name of a popular book"
date: 1982
| Franks Charles
| Franks Steve => FavoriteBook
author: "fred marko"
title: "We'll use a different name here"
date: 1960
| Name Sawyer name =>
match name
| Sawyer Tom => FavoriteBook
author: "another name"
title: "Again we change it"
date: 1999
| Sawyer Sid
| Sawyer May => FavoriteBook
author: "again another name"
title: "here is a different title"
date: 1972
------------------------------
I'm not saying all the extra symbols need to go away but when you compare
it to something like this which, for the most part, should be somewhat
clear (and less heavy-handed), the debate about whether 1 or 2 different
braces should be aligned isn't that important. The language is syntax heavy
and we do the best we can.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#1925 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAC3n6LhrOgW9LRSgllKDq2B9j6t1RHVks5rgP0fgaJpZM4MKvkp>
.
|
Okay. I think I've done my part trying to defend the stylistic part of the proposal and people who don't like it still won't like it. Having said that, I don't know where the consensus stands as far as the technical part of this being supported by the compiler. I haven't seen any comment which states making this legal is problematic in any fashion aside from something stylistic. Let me know if I need to update the RFC in any other ways. |
If this gets approved, I hope there will be an associated rustfmt RFC to helps folks that may either approve or disapprove this style: https://github.com/rust-lang-nursery/fmt-rfcs |
I don't think we would recommend them - we don't recommend a leading comma style, for example. I know @joshtriplett favours leading newlines with punctuation, e.g.,
But that is not what is being proposed here. I can't think of anywhere we are proposing actually leading with punctuation. In general, I would be in favour of this RFC - I usually prefer more flexibility in our syntax rather than less, but I don't think we would make this the default style. In particular, the needing a (Do we allow trailing |
@mdinger For the sake of demonstrating the smallest incremental change, I'd suggest changing the examples so they still indent match arms as normal (four spaces in front), rather than outdenting them and treating the vert-and-space as their indent. We could evaluate the latter as a potential style change, but that leads to the issue with trailing braces. Let's not conflate that style change and associated brace indentation issue with the minimal grammar proposal. You could add a section towards the end mentioning that potential style change, move the discussion of the brace problem from the drawbacks to that new section, and punt the actual style, arguing that the grammar change is useful independently. |
I don't mind doing that. I might get around to it a day or so but if not then, it might be a few weeks. Might be pretty busy and don't know what my free time will be like. |
I think the reason to make this a recommended style would be to avoid the double indentation and so forth. But I agree it's a separate thing-- still it seems like it'd be good to take this idea into account when settling on the best indentation style for matches |
In elm you would do something like
Which would be legal rust syntax and make everything align. |
In the interests of moving this forward: I'm not seeing any objections to the idea of supporting this in the grammar. I do see some comments about what our recommended style should look like, and in particular whether or not this should replace indentation of match arms. The thing that's causing those two to be conflated is that the RFC's samples all assume a particular style when writing, and not everyone is a fan of that style. As far as I can tell, every objection I've seen is objecting to the indentation style as shown in the RFC, which I wouldn't argue with. Does anyone object to allowing the optional vert at all? If not, I would propose that we move to accept this, and then separately determine how it might affect the default style. (And with my style team hat on, I'd be inclined to not change the recommended indentation, but instead just add the starting vert for alignment purposes when writing a series of patterns one per line.) |
@est31 I meant to disable them. Not someone removing their reaction. |
@eddyb ah right, that seems impossible indeed. |
We definitely don't make decisions on the basis of thumb counts. Obviously they can have an impact, but we try to make decisions in a more nuanced manner. Here are reasons that I personally thought the RFC should be merged:
Speaking more general, RFCs are a deliberative process, not an up or down vote. The point of the process is to seek out useful commentary from community members, and while thumbs provide some feedback for decision making, they're limited in their impact in proportion to the amount of information they contain. |
@liigo Small procedural note:
This is not a core team RFC, but a lang team one. |
I realize I'm a bit late, but I just saw this on TWiR. I haven't seen the following point: from what I understand one of the barriers for people to get into Rust is the syntax, and those people would probably be even more put off if they encounter this kind of style disagreement upfront (this syntax is being approved because people want to use it, and when it is stable it may end up on popular posts and even on official docs) and since they are not into Rust yet, most won't participate in the RFC process. To summarize that, I think this has some (potentially) negative impact on people outside of Rust who don't participate on the RFC process. Personally I just find it a bit ugly and confusing, my instinct is to expect something before a |
I just saw this in TWiR - this is a really weird syntax..... |
(Somehow I missed this one in the last couple of weeks of TWiR or I would have commented earlier. I don’t really get the motivation for this and I feel the drawbacks section being empty is unreasonable, but let those pass for now.) One concern I have about this is the impact on macro rules. As it stands, we have the
If you want to support the
(Side note: we really need After this change, people writing macros that want to accept match patterns will need to prefix You may say that trailing commas already have this problem (hence the pattern One of the main problems with increasing grammar complexity is in macros that wish to support or emulate the full grammar with certain variations of behaviour; unless we have a fragment specifier which handles that part of the grammar, macro rules become steadily more complex, or become incomplete (leading to frustration when the macro doesn’t work with otherwise normal Rust code). After this RFC is implemented, full support for match patterns in a macro are going to be
|
i really dislike the idea to have many syntax options for the same think. It makes it imo harder for a beginner to understand the language since it raises questions like |
Ppl don't use match x {
Hello |
World |
AndThings => ...,
} ? You know we have this same "issue" with Wonders how long until Rust decides to deprecate this The only nice thing about this is that now you can "convert" C into Rust with a simple |
@SoniEx2 i am curious as to what issue you are referring to when you said
the second comment about converting C to rust has been true even with out this addition. |
if
x
|| y
|| z
{ Are we gonna allow this next, for consistency? if
|| x
|| y
|| z
{ I still prefer the apparently much dreaded if
x ||
y ||
z { I have absolutely no idea why this is so dreaded. It's even more compact! |
I seriously hope that no one would consider the additional bars in if as okay. also I am pretty sure the best i can do now against this feature is make a Clippy lint against it. |
I'll take the clippy lint tbh |
@warlord500 indeed, inside an expression, leading |
It seams like more and more people don't like it, from time to time.I just give you three results:[2017-08-21]15 👍 VS 28 👎 comment [2017-08-28]comment #are_u_still_open_and_listening [2018-04-14] THE LATESTAnd forgive me, I'll repeat my old comment, for the last time:
|
The feature was stabilized and shipped in the 1.25.0 version of the Rust compiler/language. You won't be able to remove it now due to the stability promise, except of removing it from the 2018 edition but I'd say that is very unlikely... It'd require a separate RFC and good reasoning (not just thumbs). So fuming is a bit pointless I'd say. |
I think the fuming should be seen as a signal to the language team that they might want to be more careful about adding such features in the future. Perhaps the down votes should be seen as a "Hey, let's stop and reevaluate this — out of the people who voted, a vast majority didn't like this idea". A good next step would be to wait until someone presents proper arguments against the idea. Accepting an idea by default seems bad to me. |
That's what @withoutboats did in a comment last year after the first call for FCP:
That resulted in re-opening the discussion. Posts between the first and the second FCP mostly addressed concerns about trailing vertical bars. Trailing vertical bars were removed from the RFC. In the second FCP itself, two people said they were against it. One gave no reason. The second gave two: that it was weird syntax, and that there were more thumbs down than thumbs up. The thumbs up/down is not a voting mechanism, and in this case it was also hard to know whether the thumbs down were primarily about leading or trailing vertical bars. As for the growing number of thumbs down, I can't know whether a new thumbs-down means "I'm against leading vertical bars" or "I'm annoyed with the language team" or something else. And neither can the language team. A comment conveys a lot more information than a thumbs-down. I think the language team did a fine job of listening to the community on this PR. It may be that
I don't know the answer to the first two points. The answer to the third point is of course "Yes, the process could be improved." Because anything can be improved. And I do think the process is currently excellent. (I also believe that treating thumbs up/down as a vote would be a move in the wrong direction: online polls are not a good way to count votes, let alone to reach a rough consensus.) |
@mgeisler like any kind of feedback, complaining and thumbs down have the most effect the earlier on you are in the process. After a feature has been stabilized and even shortly before it is being stabilized, complaints have the least effect. You didn't even define what "such features" means... how should the language team know that? What I suggest for you in the future is that you read the RFCs opened and voice your concerns, or even just 👎 the RFCs you don't like. The |
|
I think this thread has outlived its usefulness. |
This was written to provide a formal proposal to allow a leading
|
at the beginning of amatch
as was discussed in #1745. In that thread, it was previously stated the language team is in favor of this change.Some of the sections I don't know anything about but I tried to fill in what I knew. For such a small topic, this form seems excessive and so a lot of the text seems fairly brief to me. Also, I'm not actually sure all the examples are needed but I thought it wouldn't hurt to add them.
In brief, this is a proposal that the following
match
syntax be legal:Rendered