-
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
RFC: Replace ref
by *
in patterns
#742
Conversation
+1 from my part, though it doesn't seem like people are very receptive towards syntax changes post-alpha. |
I like this because it makes more patterns work by destructuring, although that can't be done with |
When I was first learning Rust, this is what I tried to do instead of using On Tue Jan 27 2015 at 10:13:30 AM Russell Johnston notifications@github.com
|
When I was learning Rust, I thought |
I wouldn't mind replacing |
|
||
Note that most common usage of `mut` would be `mut *x` (corresponding to current `ref mut x`), | ||
so the raw-pointer-resembling `* mut` wouldn't appear very often | ||
(in rust source `ref mut` appears 139 times, compared to only 9 usages of `mut ref`). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
git grep 'mut ref'
actually shows that although there are nine occurrences of the phrase mut ref
in the Rust codebase, the first is just part of a list of keywords, the next four are examples in documentation (using &mut ref mut
, which seems to be redundant), the next is a substring of mut reference
in a comment, the next is in legitimate code, the next is a substring of mut ref_kind:
in a function declarations’s argument list, and the final one is in a test. Of all of those, I think only one really counts, and only six are actual uses of mut ref
at all.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Furthermore, none of them are equivalent to let mut x: &T = &val;
: mut ref <ident>
isn't a valid pattern. (They're all parsed as &mut (ref ...)
, i.e. pattern matching through a &mut
.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I didn’t know that mut ref <ident>
wasn’t even a valid pattern! The RFC should probably be updated to reflect that, then, given that it suggests *mut
as a replacement.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So it wasn't only me who thought that mut ref
is a valid pattern! I've updated the RFC.
I think |
I like the idea, but I’m worried that it will turn out to be confusing. Also, I wonder how this would interact with RFC #495, where subslice patterns require match &mut [1, 2, 3] {
&mut [a, mut *b..] => { ... },
...
} where the |
👎 This change just does my brain in. In pattern-matching, you're supposed to be destructuring things. So when I see Not only does this not have anything to do with unsafe pointers, it's also adding a level of indirection, which is the outright opposite of how all the other type constructors work in a pattern. As Pedro would say: very no! |
@DanielKeep |
Actually, unsafe pointer types are |
@bfops Notice that when you write @suhr You could read @P1start And I agree that |
-1 They way that Coincidentally it works the same way as If I'm wrong with this I would at least only rename |
@nwin I tried to explain to myself that both One other possibility that in my opinion would make |
👍 This makes more sense to me, and seems more consistent. |
I'm for it. We're still alpha. Consider the symmetry between the pattern and the assert in these matches:
The current syntax does not maintain the symmetry, and has a syntax error in the assert:
The proposed syntax would maintain the pattern nicely:
|
👍 This is an improvement on the status quo. IMO, we can try to fix "mut" in a different RFC, if people feel that it is still problematic after this change. |
As @krdln says, this is not a new proposal. Overall, though, I feel like this debate was settled long ago and I don't see any pressing reason to revisit it. Update: Corrected name of RFC author. |
@nikomatsakis I have quite opposite feeling – from what I've read in this discussion, I can't see an actual decision. Maybe I've missed something? Also, this RFC additionally addresses the |
@krdln this decision far predates the RFC tracker or other discussion. Here is the thread where discussion began. As you can see, it began with a proposal (by me, in fact) to use |
In the triage meeting today we decided to close this RFC. A syntactic change of this magnitude seems out of scope at this time, and the current syntax was agreed upon long ago. |
Change
ref
in patterns to*
, to make pattern syntax better match expression syntax. Example:Some(ref x)
becomesSome(*x)
. This change removes all the usage ofref
keyword, so the RFC also discusses the fate of it.Rendered
This is not a new proposal, because discussions (eg. here, here and there) about this change happen from time to time, but I think it deserves an RFC.