-
Notifications
You must be signed in to change notification settings - Fork 89
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
Idea to reenable match expressions #116
Comments
How does this impact using break or continue in when blocks? (which i believe wa the motivation to change them to statements) |
@ljharb In the above proposal, blocks in case statements are only supported in statements, not in expressions. Therefore, the problem does not arise. |
I'm really happy to see this. I worry about adding more statements to an already statement heavy language. |
Just a thought: if you made the values only non-sequence expressions, you could gain everything lost by just leveraging For what it's worth, a bare parenthesized |
@isiahmeadows Agreed that this should combine well with do expression, but it also doesn't depend on them. |
@littledan I'm implying it could be simplified by depending on them for returning statements. Alternatively, you could make it like arrow functions and do either a block or an expression (and IIRC this variant has precedent in Rust). Something like this: case (x) {
when 1 -> expr,
when 2 -> { /* block */ },
} This could be useful in both statement contexts (obvious) and expression contexts like this: let value = case (x) {
when {value} -> value,
when _ -> { throw new Error("fail") },
} |
what if the value had to be an expression, and people could use do expressions to put statements in? it feels like we're missing a lot with the current iteration, especially to those of us coming from other languages with pattern matching. const value = case(x) {
{ t } -> v,
[asdf] -> do {
console.log('stuff');
thing(5);
},
}; |
What's the status of this suggestion? It seems to be broadly supported, judging by the above, so it would be nice to update the spec text to match. :) I think it'd be a pity for expressions in branches not to be supported in a pattern-matching proposal, since the motivation of pattern matching stems from functional principles and I suspect the expression form is the most important in that context (the Redux example in the README illustrates this well). |
@FireyFly There is language precedent for making it statement-based: Swift. Their |
I've got an alternate idea: what if you separated the pattern matching statement from the pattern matching expression? Specifically:
This would dodge the |
@isiahmeadows isn't that precisely the suggestion in the topmost post in this issue? |
@FireyFly Partially.
|
This is fantastic and I'll be updating the spec to add the expression form! Thank you! |
This is implemented in #140 |
#140 seems to be about let/const/var vs when; it might be helpful to have the expression stuff implemented in a separate PR for comparing/contrasting? |
Oh! You're right. I thought I'd bundled them together. Turns out I Just Did™ the changes directly: 9434e14 So I'm just gonna close this one. Cheers~ |
The current proposal makes pattern matching based on anything after the
->
being a statement, and the wholecase
construct is a statement. The motivation for this limitation is largely to avoid the previous iteration's use of blocks in statement context, which would raise all the questions of do expressions (most controversially, the use of completion values).I think we could still support match expressions without involving completion values. The idea here would be to make the thing after
->
have a similar grammar to the thing after the=>
: It can either be a block or an expression. If allwhen
clauses in acase
construct are expressions, then the whole thing can be used as an expression. Object literals on the right hand side of->
need to be parenthized, as with=>
.Examples
From the README which work well as expressions:
(Sorry, some of the following examples are pretty stupid)
As a statement,
case
would permit a mixture of blocks and expressions (why not?):As an expression, no blocks would be permitted on the right side of
->
, e.g., the following would be a syntax error:Whereas it would not be a syntax error if all of them are expressions:
Relatedly,
return
can only be used within a block, not loose within->
:Future evolution
when 1 -> do { }
and go from there.->
. Presumably, as part of the end game, we'd also relax things to allow you to use blocks incase
expressions, with do expression semantics.The text was updated successfully, but these errors were encountered: