-
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
Explicit refutable let
: let
..else
#373
Comments
I don't like the idea of having to scan ahead in the program text for Is there some way we could revise our |
(also, I'm worried about the RFC repo issue database creeping into http://discuss.rust-lang.org/ territory ... if you are unhappy with the discuss interface, or think that it is not a good area for proposing sketches of language features, then we should discuss that and try to address it.) |
I agree, this is the wrong place for "ideas", something like this belongs in the discourse forum. |
Most of the time this should be apparent from the pattern itself, though. I asked @aturon about that on IRC:
Perhaps I phrased my question, or interpreted the response incorrectly, but this in particular falls under the latter. I don't want to have a design discussion about this right now in preparation for an RFC (I intentionally want to "get it off my plate" so I can work on more important things); I just want there to be a public record of the idea where others can see it and it doesn't get lost. I feel that a GitHub issue is more appropriate for this than the forum. (And in any case, if I have to expend more effort on thinking about where to post things than on the thing I want to post... feel free to move it.) |
I think the policy on this is a work in progress (since we just started using this issue tracker a couple of weeks ago). We migrated a bunch of stuff from the We've been having a bit of forum/process churn, so it sounds like the Rust Team needs to nail down (and write up) the policy for what goes where. Anyway, sorry @glaebhoerl for the policy pushback; my fault. |
Filed #375 for the meta-discussion. |
In the meantime Swift has added an analogous feature in the form of Another way to express this functionality which recently occurred to me would be to invert
So where EDIT: This would also extend cleanly to |
Another idea would be something like this:
or:
Meaning, the thing in the "unwrap_or" is an expression that gets evaluated (meaning "continue" in itself is an expression). Like "continue" being a value.
Just an idea. |
Is there an RFC related to this issue? I'd really love to see this feature. |
There was: #1303 |
Modernized let-else RFC: #3137 |
Should this be closed in favour of rust-lang/rust#87335 ? |
closing as #3137 has been accepted. |
Our
let
s are currently irrefutable. This is good. We could also have refutablelet
s with an explicitelse
clause:In the simplest formulation, there would be a single
else
clause with the argument being anything of type!
(break
,return
,panic!
, etc.). More sophistication is possible with chainedelse
clauses, where multiple RHSs are tried in succession, with only the last one required to be of type!
; and by also allowing a literal or constant which definitely matches rather than just an!
(so in the above example,else Ok(9)
, for instance).The use case for this overlaps with
if let
, but not completely.if let
involves rightward drift, and is appropriate if you want access to e.g. the value in anOption
for a short period, and then to do other things afterwards, or if you want to handle theelse
case by something other than exiting.let
..else
avoids rightwards drift and is appropriate if you want to early-escape from the whole computation when the pattern doesn't match.The potential for syntactic ambiguity with
if
..else
needs to be thought through. But I don't believe there is an actual problem:If we interpret this as being an
if
expression without anelse
block, and theelse
block as belonging to thelet
, that ends up being contradictory:if
expressions have type()
, which is never refutable, and so doesn't make any sense as the type of PAT in a refutablelet
. So the only sensible way to interpret this is as anif
-else
expression on the RHS.Earlier discussion on the mailing list.
The text was updated successfully, but these errors were encountered: