-
Notifications
You must be signed in to change notification settings - Fork 7.9k
[RFC] Implement block expressions - alternative implementation #5407
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
Conversation
Yes, I agree that this is the better approach. As you mention in #5403 (comment) we can't really get around treating match expressions and match statements slightly differently, if only due to the trailing semicolon. I wonder whether |
I would say no for the same reason and argue the same thing for arrow functions. |
I think I agree. As match is the only syntax we'll be accepting that can be used in both statement and expression context, it makes sense to limit the use of blocks expressions to that context as well, at least initially. |
I think this is the best approach. I'll adjust this in the RFC and implementation. Thanks for your assessment! I have one more question: What do you think about omitting the match ($x) {
...
} // No semicolon necessary It does make the grammar pretty complicated (same issue as with the first block PR). Additionally there's an ambiguity when nesting $x = match ($y) {
default => {
// Is this a statement or expression?
match ($z) { ... }
}.
}; With the current implementation the inner On the other hand I think requiring the semicolon would be incredibly irritating. |
So far, there're some problems with goto and break inside of match expressions where the return value is used - #5371 (comment) Supporting the general case of statement expressions would require solving the same issues (or almost the same) as |
I'm closing this PR as we decided to go a different route. But we'll definitely have to fix |
What do you guys think about this syntax: match ($x) {
default => {
echo 'Foo';
echo 'Bar';
<= 'Baz';
}
}
|
There's also
The other things I can think of would conflict with other names Right now, the only ambiguous case of having a semicolon is |
I thought about
Since |
Conflicts: Zend/zend_compile.c Zend/zend_language_parser.y Cherry-picked from php#5407 (written by Ilija Tovilo) and patched by Tyson This also changes the block expression syntax to ({}) outside of arrow functions for the convenience of getting the last "expression" in a REPL.
As promised here is the alternative implementation of #5403.
Just to reiterate, this PR is different in that it doesn't touch the statement list (
'{' inner_statement_list '}'
) we already have. This makes things much easier but it does still require specifically allowing statement lists in the arrow function and match grammar, so it doesn't really help in that regard.@nikic Do you agree this is the way to go?