On 12 April 2020 01:16:04 BST, Ilija Tovilo <tovilo.il...@gmail.com> wrote: >Hi internals > >I'd like to announce the match expression RFC that replaces the switch >expression RFC I announced a few weeks ago. >New: https://wiki.php.net/rfc/match_expression >Old: https://wiki.php.net/rfc/switch_expression
Hi Ilija, Thanks for continuing to work on this, I think the new syntax feels much more natural when introduced with a new keyword. > Additionally, match arms can now contain statement lists >which allows you to use the match expression anywhere the switch >statement would have been used previously. I quite strongly disagree with this part, however. I don't think it's necessary for the new keyword to be a replacement for every switch statement, any more than switch replaces every if statement or vice versa, and doing so adds a lot of complexity to the proposal. In languages where expression blocks with an implicit return are a standard feature, the combination works naturally, but without that we have to add a bunch of special rules: - Can match arms contain other flow control, such as "return" or "continue"? What if we later want to give those special meaning? - Can I mix a bare value and a block as arms of the same match, or does the whole construct need to be in either "expression form" or "statement form"? Will this restriction be easy to explain to users? - Can I omit the trailing semicolon even in "expression form", i.e. if my branches have no braces around, as long as I don't use the result? What will the error message look like if I get this rule wrong? I also think the use of => and , feels natural when pairing an input to an output, but odd when pairing a test to a block of imperative code. In the "expression form" (no braces, right hand side is an expression evaluated if the branch matches), it matches array syntax (right hand side is an expression immediately evaluated as a value) and short closure syntax (right hand side is an expression evaluated when function is executed). In the "statement form", its closest relative would be if-elseif-else, but only if we wrote those like this: if ($score==10) => { echo "Congratulations"; }, elseif ($score==9) => { echo "Nearly there"; }, else => { echo "Keep trying"; } Switch statements are different again, but their ancestor is not if blocks but goto labels, which explains both the syntax and the fall-through behaviour: goto "case_$score" else "default"; // imaginary dynamic goto becomes "switch ($score)" case_10: echo"Congratulations"; goto end; // becomes "break" case_9: echo "Nearly there"; goto end; default: echo "Keep trying"; goto end; end: // becomes closing brace of switch block The inability to nest ternary ?: without parentheses makes the single expression form particularly useful in PHP, so I'd love to see the RFC focus on that. We can always add statement blocks in later, either as a special case as currently proposed, or because we're adding them elsewhere, and perhaps with a way to return a value rather a requirement not to use it. Regards, -- Rowan Tommins [IMSoP] -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php