Re: Strict switch statements

From: Date: Thu, 14 Jun 2018 11:32:01 +0000
Subject: Re: Strict switch statements
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-102256@lists.php.net to get a copy of this message
On 14.06.2018 at 10:35, Nikita Popov wrote: > On Thu, Jun 14, 2018 at 6:53 AM, Sara Golemon <pollita@php.net> wrote: > >> Just for casual discussion at this point: >> https://github.com/php/php-src/pull/3297 >> >> switch ($a) { >> case FOO: >> // Works exactly as current behavior. >> break; >> case == FOO: >> // Nearly identical, though not using the ZEND_CASE optimization. >> // Can probably make this equivalent to case FOO, but >> it felt >> like an interesting direction. >> break; >> case === FOO: >> // Only triggers if $a === FOO, no type juggling >> break; >> } > > 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. […] ACK. Even the example shows the gotchas with this kind of mix: the final case would never match, or would it (which I would find even more confusing)? Furthermore, I personally don't like switch statements in a higher level programming language, since they feel so low level (optional fall through, and whatever). And there is already the “switch (true)” hack, to make the proposed behavior possible (without optimizations, though, I presume), see <https://3v4l.org/qPauK> and <https://3v4l.org/iSLkW>. > 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" => {...}, > } On a quick thought, I like this! -- Christoph M. Becker

« previous php.internals (#102256) next »