Re: Strict switch statements
| From: | Alice Wonder | Date: | Thu, 14 Jun 2018 16:16:58 +0000 |
| Subject: | Re: Strict switch statements | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-102264@lists.php.net to get a copy of this message | ||
On 06/14/2018 08:57 AM, Thomas Bley wrote:
Nikita Popov wrote on 14.06.2018 10:35:Should declare(strict_types = 1) do that? I haven't tried, but I would think it should.On Thu, Jun 14, 2018 at 6:53 AM, Sara Golemon <pollita@php.net> wrote:for simplicity I would use an extra parameter to have a strict comparison (similar to the extra parameter in in_array()): switch ($a, true) { // strict comparison switch ($a) { // loose comparison Regards ThomasJust for casual discussion at this point: https://github.com/php/php-src/pull/3297 switch ($a) { case FOO:I like the general idea here (switch with strict type comparison), but not super fond of the particular syntax and implementation. I think if people want to use strict matching, they'll quite likely want to have it on all cases. Something like "strict switch ($expr) {}" or "switch strict ($expr) {}" or "switch (strict $expr) {}" or "switch ($expr) strict {}" or "switch ($expr) { strict; }" or whatever would be preferable in that case. Additionally, switch has the issue of fall-through behavior, which is somewhat unexpected and error prone to many people. It might make sense to introduce an entirely new "match" statement that conforms a bit more with how switch-like strictures are implemented nowadays. That is, something like match ($expr) { "foo" => {...}, "bar" | "baz" => {...}, } or similar. This might need some more design work to ensure forward compatibility with potential future algebraic datatypes etc. Nikita// Works exactly as current behavior. break;case == FOO:// Nearly identical, though not using the ZEND_CASE optimization. // Can probably make this equivalent tolike an interesting direction.case FOO, but it feltbreak;case === FOO:// Only triggers if} Love it? Hate it? See obvious flaws? The implementation is just a rushed proof of concept, not something I've labored over, it may well have bugs. Certainly wouldn't target anything earlier than 7.4, if at all.$a === FOO, no type juggling break;