Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:102264 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 5962 invoked from network); 14 Jun 2018 16:17:05 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 14 Jun 2018 16:17:05 -0000 Authentication-Results: pb1.pair.com smtp.mail=alice@librelamp.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=alice@librelamp.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain librelamp.com designates 45.79.96.192 as permitted sender) X-PHP-List-Original-Sender: alice@librelamp.com X-Host-Fingerprint: 45.79.96.192 librelamp.com Received: from [45.79.96.192] ([45.79.96.192:53544] helo=librelamp.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 43/1D-29356-EF4922B5 for ; Thu, 14 Jun 2018 12:17:03 -0400 Received: from localhost.localdomain (unknown [IPv6:2600:1010:b00f:90e4:3087:d890:629a:ea3d]) by librelamp.com (Postfix) with ESMTPSA id 6109D6F1 for ; Thu, 14 Jun 2018 16:16:59 +0000 (UTC) To: internals@lists.php.net References: <20180614155730.2C8BC1A8027C@dd1730.kasserver.com> Message-ID: <53d1a1f6-10b8-3aec-c2ac-a005d6041f15@librelamp.com> Date: Thu, 14 Jun 2018 09:16:58 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0 MIME-Version: 1.0 In-Reply-To: <20180614155730.2C8BC1A8027C@dd1730.kasserver.com> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Strict switch statements From: alice@librelamp.com (Alice Wonder) On 06/14/2018 08:57 AM, Thomas Bley wrote: > Nikita Popov wrote on 14.06.2018 10:35: > >> On Thu, Jun 14, 2018 at 6:53 AM, Sara Golemon 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; >>> } >>> >>> 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. >>> >> >> 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 >> > > 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 > Thomas > Should declare(strict_types = 1) do that? I haven't tried, but I would think it should.