Re: Adding explicit intent for SWITCH/CASE fall through?
| From: | Rowan Tommins | Date: | Sun, 20 Oct 2019 11:28:35 +0000 |
| Subject: | Re: Adding explicit intent for SWITCH/CASE fall through? | ||
| References: | 1 2 3 4 5 6 7 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-107587@lists.php.net to get a copy of this message | ||
Hi Mike,
On 20/10/2019 01:39, Mike Schinkel wrote:
Not yet having experience working on PHP's parser I do not know how it is configured nor what about the design of PHP requires keywords unable to be used as constants, class names, function or method names. I know this is a tangent but that seems like a requirement designed to simplify the parser maybe, and not because of any logical requirement. So I assume whatever word was used for this would also have to be a reserved keyword? Or not?I don't know the full implications in the parser, but at the language level, any constant can technically appear where this keyword would. That is, the following is valid right now: const fallthrough=null; fallthrough; It doesn't do anything, but it's valid because any expression on its own is a valid statement, and a constant on its own is a valid expression. Interestingly, that actually makes a single keyword *more* appealing to me, because it means you can get *forwards* compatibility by writing this: if ( PHP_VERSION < 80000 && ! defined('fallthrough') ) {
define('fallthrough', null);
}
switch ( $x ) {
case 1: stuff(); fallthrough; case 2: moreStuff(); break;} For this common case where the new keyword is just a no-op, you then have code that supports and works identically across a range of versions: * On versions without the keyword, the constant expression is a no-op (and may even be optimised out by OpCache?) * On versions where the keyword is present but optional, the define is skipped, and the keyword is a no-op * On versions (or external tooling) where omitting the keyword is a warning, or even an error, the keyword indicates intent Regards, -- Rowan Tommins (né Collins) [IMSoP]