Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:102281 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 22383 invoked from network); 16 Jun 2018 14:45:33 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 16 Jun 2018 14:45:33 -0000 Authentication-Results: pb1.pair.com smtp.mail=bjorn.x.larsson@telia.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=bjorn.x.larsson@telia.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain telia.com designates 81.236.60.156 as permitted sender) X-PHP-List-Original-Sender: bjorn.x.larsson@telia.com X-Host-Fingerprint: 81.236.60.156 v-smtpout3.han.skanova.net Received: from [81.236.60.156] ([81.236.60.156:58357] helo=v-smtpout3.han.skanova.net) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id F6/29-29356-C82252B5 for ; Sat, 16 Jun 2018 10:45:32 -0400 Received: from [192.168.7.8] ([213.64.245.126]) by cmsmtp with ESMTPA id UCSGfm3ri8KmIUCSGfRZ9X; Sat, 16 Jun 2018 16:45:29 +0200 To: Rowan Collins , Nikita Popov , Sara Golemon Cc: PHP internals References: Message-ID: Date: Sat, 16 Jun 2018 16:45:32 +0200 User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.8.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: sv X-CMAE-Envelope: MS4wfDBsmKVqROXJCH0QkdUXIdjoGgJGOraAfh1rdHzv7fHxJBpULTYkMYd3NOIwCOHihLUOLiJio0+vjApMpOkpgrI/zxdG5FJqiSpQoSZXHLHPPmdA8Q4X ss1WwfCoD6HRzIKNJK4Mbe9ivgW7npdpGeWH1GQehiqOln3iEPnbUIVi717riK3/FpFeXzmmY16L4+c++i7Avtk7oZvsPAiPQbJ6BWNcR4LrKIisf/BFtmuj 5GBFOWC8ZO6o0B5K6ELW6ofY6kuXoj01dwF6qeTwjL4= Subject: Re: [PHP-DEV] Strict switch statements From: bjorn.x.larsson@telia.com (=?UTF-8?Q?Bj=c3=b6rn_Larsson?=) Den 2018-06-14 kl. 11:21, skrev Rowan Collins: > On 14 June 2018 at 09:35, Nikita Popov wrote: > >> 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. >> > > For ages, I've had an idea along these lines kicking around. Like Nikita, > I was assuming that all branches would want strict comparison or none, so > was thinking of putting the operator at the top, something like "switch > ($expr) use (===)". I figured it could then be extended so the "===" could > be substituted for any binary operator; for instance, you could match > values into ranges with "switch ($score) use (>=)". > > That said, I can see an attraction in a form with the operator on the > branch, if operators other than "==" and "===" are supported; for the range > example, it might be handy to have cases for ">= 50", "> 0", and "=== 0". > > I prefer reusing "===" in some way over the word "strict", even if no other > operators are supported, because "strict" could mean a number of things - > "strictly no fall-throughs", for instance - and I think that could cause > confusion (people already expect "strict types" to mean more than it does). > > Regards, Yup, I also like this idea very much. Using the === syntax is a clear message on what's it all about. Having the ability to extend it to other operators is also interesting, e.g. !== & !=. Another point is that by keeping switch keyword it would be relatively easy to upgrade legacy code, making it more robust. Of course there is a use for a new keyword like match extending the functionality even more. Maybe we can have both? I'm thinking if one should be able to have like: switch ($a)  {     case === FOO:         break;     case !== BAR:         break; } or: switch ($a) with !== {     case FOO:         break;     case BAR:         break;     default:         break; } or: switch ($a, ===)  {     case FOO:         break;     case BAR:         break; } r//Björn Larsson