Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:102261 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 1116 invoked from network); 14 Jun 2018 15:57:34 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 14 Jun 2018 15:57:34 -0000 Authentication-Results: pb1.pair.com smtp.mail=mails@thomasbley.de; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=mails@thomasbley.de; sender-id=unknown Received-SPF: error (pb1.pair.com: domain thomasbley.de from 85.13.128.151 cause and error) X-PHP-List-Original-Sender: mails@thomasbley.de X-Host-Fingerprint: 85.13.128.151 dd1730.kasserver.com Received: from [85.13.128.151] ([85.13.128.151:53489] helo=dd1730.kasserver.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 1B/1C-29356-D60922B5 for ; Thu, 14 Jun 2018 11:57:33 -0400 Received: from dd1730.kasserver.com (dd0802.kasserver.com [85.13.143.1]) by dd1730.kasserver.com (Postfix) with ESMTPSA id 2C8BC1A8027C; Thu, 14 Jun 2018 17:57:30 +0200 (CEST) MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-SenderIP: 79.211.25.47 User-Agent: ALL-INKL Webmail 2.11 In-Reply-To: References: To: pollita@php.net, nikita.ppv@gmail.com Cc: internals@lists.php.net Message-ID: <20180614155730.2C8BC1A8027C@dd1730.kasserver.com> Date: Thu, 14 Jun 2018 17:57:30 +0200 (CEST) Subject: Re: [PHP-DEV] Strict switch statements From: mails@thomasbley.de ("Thomas Bley") 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