Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:109297 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 28591 invoked from network); 25 Mar 2020 17:41:56 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 25 Mar 2020 17:41:56 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id D8EB41801FD for ; Wed, 25 Mar 2020 09:06:37 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS24940 144.76.0.0/16 X-Spam-Virus: No X-Envelope-From: Received: from mail01.nexxes.net (mail01.nexxes.net [144.76.185.252]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 25 Mar 2020 09:06:36 -0700 (PDT) Received: from [IPv6:2001:4dd7:2167:0:e083:af92:b312:4556] (2001-4dd7-2167-0-e083-af92-b312-4556.ipv6dyn.netcologne.de [IPv6:2001:4dd7:2167:0:e083:af92:b312:4556]) (Authenticated sender: db220660-p0g-1) by mail01.nexxes.net (Postfix) with ESMTPSA id 342EC86063B for ; Wed, 25 Mar 2020 17:06:34 +0100 (CET) To: internals@lists.php.net References: <047092C7-84FB-42AB-8084-7B83F76F55C1@me.com> <4513B88E-CA5B-4DF2-94C8-242BEE54ADCC@me.com> <767910ef-d27b-404c-9ad1-037105c69d12@www.fastmail.com> <2E98378A-605D-40ED-898C-6ABBFBD091B9@me.com> Autocrypt: addr=php@dennis.birkholz.biz; prefer-encrypt=mutual; keydata= mQGiBEnM7DIRBADL+i+aPpq2kqIJbf9Lhgu5Ly8Df2AJpIZnEVT0+vlTyCf1TzX6th5NW1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7Q/lla6KFAu9 n1c= Message-ID: <13054915-92e1-2723-2db7-fa13376ffecb@birkholz.biz> Date: Wed, 25 Mar 2020 17:06:33 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] [RFC] switch expression From: php@dennis.birkholz.biz (Dennis Birkholz) Hello together, Am 25.03.20 um 16:46 schrieb Larry Garfield: > On Wed, Mar 25, 2020, at 10:29 AM, Ilija Tovilo wrote: >> Thanks for your feedback, Larry! >> >>> One possible improvement to either version is allowing an expression on the left side. That is, rather than doing an equality match, do a boolean match. >> >> This is how Rust does it: >> >> ```rust >> let x = match ... { >> Some(y) if y < 5 => ... >> } >> ``` >> >> In other words, you can add an additional guard to each case that >> excepts any expression. We don't really benefit a lot from that since >> we don't have pattern matching. I don't think this would add any >> significant benefit over: >> >> ```php >> $x = true switch { >> $x !== null && $x < 5 => ... >> } >> ``` > > Good point, I'd forgotten about that potential trick. So as long as an expression is allowed on the left, rather than just a literal, which is then == compared against the provided value, that should be "good enough" for most use cases. > > The implementation should include some tests to make sure that works properly, but I'm happy with the resulting syntax. > > So then the net result is: > > $var = switch($val) { > case expr1 => expr2; > } > > Where $val gets compared against the result of each expr1, and if true then $var is set to expr2. on the first glance this all looks nice but you actually created something more like an if-expression that uses switch as a keyword because you stripped switch of some of its major features: - you compare the given value to possible cases -> you compare expressions to true - you can fall through to other cases without break - what about the default case? What about the following if-expression-syntax: $var = if ($x > 0) { return 1; } elseif ($x < 0) { return -1; } else { return 0; } Maybe this is more in line of what you want to do with your switch expression? Greets Dennis