-
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
"Use" in patterns (to match an expression’s value) #2272
Comments
Can you clarify the semantics of |
"use the value of y here" or "use y" for short. Or we could break every program and go with "let z" instead? I'm ok with that too, but it would break every program. |
This looks like syntax sugar for match guards. fn main() {
let x = 2;
let y = 2;
match x {
_ if x == y => println!("{} {}", x, y),
z => println!("{} {} {}", x, y, z)
}
} It seems unnecessary for these small examples. |
@mfarrugi Kinda, yeah, but it follows the DRY principle (don't repeat yourself), so it's much nicer to work with. (unless you propose we always use match guards as a way to solve the context-sensitivity issue? doesn't seem like a nice solution, tho) |
(I removed the comment ("Rust has lint for that" or something) you are referring, before seeing your reply, as I noticed you are not concerned in such a way. Sorry for impeding discussion!) |
@SoniEx2 Moderator Note Please be constructive. I've noticed a pattern of you posting low-effort feature requests with little to no justification given, and then being incredibly blunt and unconstructive in the ensuing discussion. Please act civilly in Rust spaces. If you want to work with others to come up with actionable proposals I suggest discussing stuff in http://internals.rust-lang.org/ instead of here. |
Well, you haven't seen me act non-civilly, even if I may be using caps for emphasis. And yeah, the original proposal - using an expression's value in a pattern - is surprisingly not something I care so much about, now. I care more about the context-sensitivity issue. The original proposal is one fix, there are other fixes like requiring pattern guards when using values (unless they're literals) and requiring all const to be all-uppercase and all non-const (yes, even static variables) to be non-all-uppercase. There's no nice way to fix this except for some workarounds. (At least with the original proposal you can require that any match using at least one "use" must use "use" for every case, so it is a pretty good workaround if I can say so myself. We can then work towards deprecating the old syntax.) |
Moderator Note: We have markdown available, so there's no need to use capitalization for emphasis. If you are not already aware, use of all-caps is commonly equated with "shouting and other impolite or argumentative behaviors." As a fellow moderator, I think @Manishearth's note was warranted, if only to discourage future use of all-caps. |
By the way, I don't wish to litigate the original note on thread. I just wanted to make a public service announcement regarding markdown and use of all-caps. If you want more feedback on the details of @Manishearth's note, I recommend you email [email protected] |
Worth mentioning: you're working off a premise that this context
sensitivity is something that needs fixing; now everyone seems to agree
with that and you're not really addressing that.
…On Dec 30, 2017 5:38 PM, "Soni L." ***@***.***> wrote:
Well, you haven't seen me act non-civilly, even if I may be using caps for
emphasis.
And yeah, the original proposal - using an expression's value in a pattern
- is surprisingly not something I care so much about, now. I care more
about the context-sensitivity issue. The original proposal is one fix,
there are other fixes like requiring pattern guards when using values
(unless they're literals) and requiring all const to be all-uppercase and
all non-const (yes, even static variables) to be non-all-uppercase.
There's no nice way to fix this except a bunch of workarounds. (At least
with the original proposal you can require that any match using "use" be
context-free, so it is a pretty good workaround if I can say so myself. We
can then work towards deprecating the old syntax.)
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#2272 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABivSNI0J0m_RP-AODH-84Qtz4IigQjiks5tFifGgaJpZM4RPQgV>
.
|
Rust 2.0 could fix this. I'd prefer
There's also the issue of whether it's destructuring or using a value... match v {
let Some(let x) => ...,
None => ...,
}
// or
match v {
Some(let x) => ...,
None => ...,
} I think (If you meant "not" instead of "now", the issue is that the same token in the exact same place can mean either a value (often referred as "RHS") or a name (or "LHS"), and it can change even in the same file, so you can have a file where the same block of code appears twice, but on the first block it's a name and on the second block it's a value. This is especially annoying when you want your syntax highlighter to highlight the two cases differently, because it can't know which case it is. Maybe I'm weird but I'd like my syntax highlighter to tell me when I'm binding new names (or shadowing other names) without needing full static analysis.) |
Consider, in the title of the issue, whether the important part is Perhaps braces might make sense here, similar to how they're used to pass a const expression as an explicit parameter to a generic? https://github.com/rust-lang/rfcs/blob/master/text/2000-const-generics.md#applying-a-const-as-a-parameter (An eventual epoch could even switch all unbraced idents to meaning bindings, though I'm sure that would be controversial for some time.) |
I'm not talking about changing constants to be different from identifiers. I'm talking about making it clear when you're declaring new bindings. We have (Having |
use range: let x: (Option<RangeFull>,) = (Some(..),);
match x {
(Some(use ..),) => println!("got rangefull"),
_ => println!("no rangefull")
} |
Closing in favor of #263. |
"Use" in patterns would introduce a new feature and fix a small pain point in current Rust.
Currently this doesn't work:
I propose "use" in patterns so that it can be made to work:
"use" in patterns should take an expression, no reason to accept only variables if you can just put the result of any expression in a variable.
This can also be combined with destructuring:
Another effect of "use" in patterns can be summarized in "WHY'S RUST CONTEXT-SENSITIVE?!". Check out the following code (assume the author is using
#[allow(non_snake_case)]
):use
in patterns also lets you use ranges in patterns:The text was updated successfully, but these errors were encountered: