On 29/04/2020 01:26, Larry Garfield wrote:
To that end, I'm going to refer to them as rustmatch (an expression that only 
supports expressions and returns things; basically a fancier alternative to 
ternary) and switchtng (a procedural branching statement that is basically 
switch but with the lessons of the 40 years since C switch was invented, which 
PHP blindly inherited) to avoid privileging one or the other.


I like the codenames. :)

Thinking about it, I think the "switchng" syntax can be even more conservative. The RFC lists four issues with switch, of which three would be applicable to a "switchng" RFC:

1. Type coercion
2. Fallthrough
3. Inexhaustiveness

For #1, the only thing we need is some way to opt into "strict mode" on the switch statement, and have the comparisons handled by === rather than ==. That could be as simple as writing "switch strict" instead of "switch".

For #2, the problem is that fallthrough is implicit where no "break" is present, so what if we just said that reaching another "case" statement was a runtime error in a switchng statement? That would mean any existing switch statement that didn't use fallthrough could be "upgraded" without changing its body at all.

We could allow multiple case labels with no statements between, allowing for the common scenario of multiple conditions running identical code. Alternatively (or as a separate enhancement) we could allow comma-separated lists in case blocks (on both normal switch and switchng).

For other cases of fallthrough, we could add a new keyword (or recycle "continue") to explicitly go to the next case. That could even be left to "Future Scope", because anyone wanting it can always "downgrade" to the version with implicit fallthrough.

Finally, #3 can be part of the same "strict mode", with one of two behaviours:

a) Make a switchng with no "default:" clause a compile-time error
b) Make a switchng which doesn't match any case a run-time error, effectively adding an implicit clause of "default: throw new Error(); break;"


So our shiny new "switchng" statement would look ... exactly the same as a normal switch statement, but with one new keyword, and the ability to throw two new errors.


Regards,

--
Rowan Tommins (né Collins)
[IMSoP]

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to