Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upAllow negation of `if let` #2616
Comments
Centril
added
T-lang
A-syntax
A-expressions
A-control-flow
labels
Dec 22, 2018
This comment has been minimized.
This comment has been minimized.
H2CO3
commented
Dec 23, 2018
•
I don't understand what's "awkward" in a block. Furthermore, I don't see any good syntax for realizing this. Certainly, both proposed ones ( |
This comment has been minimized.
This comment has been minimized.
burdges
commented
Dec 23, 2018
|
We've seen numerous proposals for this, so maybe review them before attempting to push any serious discussion. If the variants contain data then you normally require said data, but you cannot bind with a non-match, so
Also, one can always do very fancy comparisons like:
I believe the only "missing" syntax around bindings is tightly related to refinement types, which require a major type system extension. In my opinion, we should first understand more what design by contract and formal verification might look like in Rust because formal verification is currently the major use case for refinement types. Assuming no problems, there is a lovely syntax in which some
In this |
This comment has been minimized.
This comment has been minimized.
|
I looked around for past proposals regarding expanding the @burdges Thanks for sharing that info, I have some reading to do. But with regards to
No, those are exactly what I would like, except they're not available for enum variants, i.e. for One problem with the existing |
This comment has been minimized.
This comment has been minimized.
H2CO3
commented
Dec 23, 2018
Oh, I think that can be fixed quite easily. They could be |
This comment has been minimized.
This comment has been minimized.
burdges
commented
Dec 23, 2018
|
I'd suggest
That said, if you have more than a couple variants then In fact, you'd often want the or/
|
This comment has been minimized.
This comment has been minimized.
H2CO3
commented
Dec 24, 2018
|
I made a PoC implementation of the "generate |
This comment has been minimized.
This comment has been minimized.
|
@H2CO3 This already exists on crates.io: https://crates.io/crates/derive_is_enum_variant and there's likely more complex variants too involving prisms/lenses and whatnot. It seems to me however that generating a bunch of Fortunately, given or-patterns (rust-lang/rust#54883) let chains (rust-lang/rust#53667), and a trivial macro defined like so: macro_rules! is { ($(x:tt)+) => { if $(x)+ { true } else { false } } }we can write: is!(let Some(E::Bar(x)) && x.some_condition() && let Other::Stuff = foo(x));and the simpler version of this is: is!(let E::Bar(..) = something)
// instead of: something.is_bar() |
This comment has been minimized.
This comment has been minimized.
H2CO3
commented
Dec 24, 2018
|
@Centril I'm glad it already exists in a production-ready version. Then OP can just use it without needing to wait for an implementation.
What do you exactly mean by this? AFAIK there aren't many kinds of enum variants in Rust. There are only unit, tuple, and struct variants. I'm unaware of special prism and/or lens support in Rust that would lead to the existence of other kinds of variants.
I beg to differ. Since they are generated automatically, there's not much the user has to do… moreover the generated functions are trivial, there are
Those are very nice, and seem fairly powerful. I would be glad if we could indeed reuse these two new mechanisms plus macros in order to avoid growing the language even more. |
This comment has been minimized.
This comment has been minimized.
I found this a while back: https://docs.rs/refraction/0.1.2/refraction/ and it seemed like an interesting experiment; other solutions might be to generate
In a small crate I don't think it would pay off to use a derive macro like this; just compiling
Sure; this is indeed nice; but imo, it seems natural and useful to think of |
This comment has been minimized.
This comment has been minimized.
alexreg
commented
Dec 29, 2018
|
I'm for this. It arguably reduces the semantic complexity of the language, and improves consistency, especially with let-chaining coming in. Has @rust-lang/libs thought of bundling the |
This comment has been minimized.
This comment has been minimized.
|
See also #1303. |
This comment has been minimized.
This comment has been minimized.
graydon
commented
Jan 12, 2019
|
Opposed. Not a big enough use-case to warrant extension at language level. Similar to there not being both |
This comment has been minimized.
This comment has been minimized.
|
It is respectfully nothing like the while vs do while situation. A while loop is one thing, and do while is another. An if statement already exists in the language with clearly defined semantics and permutations. This syntax reuses the if statement in name only, masquerading as a block of code while not actually sharing any of its features apart from reusing the same if keyword, with no reason why that can’t be fixed. |
This comment has been minimized.
This comment has been minimized.
graydon
commented
Jan 12, 2019
|
I regard (And definitely also not a guard-let or whatever Swift has. It has too many of these.) |
This comment has been minimized.
This comment has been minimized.
comex
commented
Jan 13, 2019
|
Which is why I think |
This comment was marked as off-topic.
This comment was marked as off-topic.
comex
commented
Jan 13, 2019
•
|
(and while this is admittedly both rude and off topic :\, after checking your recent posts, is there anything you’re not against?) |
This comment has been minimized.
This comment has been minimized.
mikhail-krainik
commented
Jan 15, 2019
|
In Ruby exists
|
This comment has been minimized.
This comment has been minimized.
|
I’ve always disliked |
This comment has been minimized.
This comment has been minimized.
vext01
commented
Feb 5, 2019
|
Hi, I've landed here looking for
Which I think today, is best expressed as:
|
christopherswenson
referenced this issue
Mar 11, 2019
Closed
Add a `let...else` expression, similar to Swift's `guard let...else` #1303
This comment has been minimized.
This comment has been minimized.
|
@mark-i-m I dislike them when they're just sugar for |
This comment has been minimized.
This comment has been minimized.
|
I feel like that would be a bit unexpected for most people. There is conceptually no reason |
mqudsi commentedDec 22, 2018
The RFC for
if letwas accepted with the rationale that it lowers the boilerplate and improves ergonomics over the equivalentmatchstatement in certain cases, and I wholeheartedly agree.However, some cases still remain exceedingly awkward; an example of which is attempting to match the "class" of a record
enumvariant, e.g. given the following enum:and an instance
foo: Foo, with behavior predicated onfoonot beingBarand a goal of minimizing nesting/brace creep (e.g. for purposes of an early return), the only choice is to type out something like this:or the equally awkward empty
matchblock:It would be great if this were allowed:
or perhaps a variation on that with slightly less accurate mathematical connotation but far clearer in its intent (you can't miss the
!this time):(although perhaps it is a better idea to tackle this from an entirely different perspective with the goal of greatly increasing overall ergonomics with some form of
isoperator, e.g.if self.integrity_policy is Foo::Bar ..., but that is certainly a much more contentious proposition.)