Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:109303 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 46251 invoked from network); 25 Mar 2020 18:27:06 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 25 Mar 2020 18:27:06 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 0EC4218050A for ; Wed, 25 Mar 2020 09:51:46 -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=-2.3 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,FREEMAIL_ENVFROM_END_DIGIT,FREEMAIL_FROM,RCVD_IN_DNSWL_LOW, SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS8560 212.227.0.0/16 X-Spam-Virus: No X-Envelope-From: Received: from mout.gmx.net (mout.gmx.net [212.227.15.19]) (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:51:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1585155103; bh=Lw3t3Sfhky+Leqoy0g0yw6dZESoYgRqeCDfWpGZZy4g=; h=X-UI-Sender-Class:Subject:To:References:From:Date:In-Reply-To; b=IQBDyeiFLNJRFjfbHK7J6YHc06nlB04uQMvdnmysPm+hbxx2kRj5V8Hhs8NeaNDQ2 QTarXeQTukl2vaMx2BV0YYdv59Jtz8hEoM2aFtwRgmwy23htpHaEVlKQOjcr/Ih+1J 9hGhe1Di/dpoh3pkM3bh1cMP+JEHEpHrH5a2BA0c= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from [192.168.2.130] ([84.179.237.201]) by mail.gmx.com (mrgmx004 [212.227.17.190]) with ESMTPSA (Nemesis) id 1MF3DW-1j1nBH1x1M-00FOmy; Wed, 25 Mar 2020 17:51:43 +0100 To: Dennis Birkholz , 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> <13054915-92e1-2723-2db7-fa13376ffecb@birkholz.biz> Message-ID: Date: Wed, 25 Mar 2020 17:51:39 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.6.0 MIME-Version: 1.0 In-Reply-To: <13054915-92e1-2723-2db7-fa13376ffecb@birkholz.biz> Content-Type: text/plain; charset=utf-8 Content-Language: de-DE Content-Transfer-Encoding: quoted-printable X-Provags-ID: V03:K1:prdycm5BZahzxc+GbdU/Y+OMUL+HaVu+E5hEuz9W4YInXajNlfP tRCXGpXHoG/olM+UAlDNKyWKWnyqHu3+qLedl/5XDZ/lUeD8NOkkvhbIVOmXHY9WE4U+QkN T5nEpnLEmHKB1Bb0dWz4lD73LgYFRwSsypZ1pM6DzPgIMsqUCL+kM3rJ8atypzdQaAeXZlI 0xSaK9I8I9wWA87Q7Fgew== X-UI-Out-Filterresults: notjunk:1;V03:K0:dkdbb8SYu2Y=:J6O/aSHjmTgGkxDtapw1Ai gjnozxuqTuHZEoP/b/wWuYiUxDNGsKAi0MALawG7aXkqRSJTFBTqZK6pd23BqW987g+g3mWIa yP/rm0cvpZv6VtwQdTUeVSBdWS8XLBE8swyZox6hC2PjD1ZawfkKyflZrPfHFlTv/OWofFkRf zZSejxxEGLvlggciTGwr7ev92PPT8Y4w2bbqLZB6nRnydLhNqCu+MTBHQ2hqnNvfaGqyXarms 4iWdz2t6KAxCPlnfa8iiwDAZAEBFxEABIP0uyk69ZgrUks5npfuBQ3KoBnM4N71kbMiYpYm8i hYJmak6SykZkYDEUG+EBGzqbzg9E8+jU2GGXwE28zHU2KQPvZKMaOV5eshQ+FfHRwmRcRSoBY SxquC6zypT/jeI3Qoassb68yH3zisEfioMsSnJeHLsloso5Z4jLHYX4X+e+GosBADTEwYFMH7 Q5W7vL0qLIozd49Dp3tB+brTyvDwRJ+hpoVziIe6yMvfszaAc3fYNGatPjbmBWQe3S2evQyKE 47Y0+I+vU8uImF2dh1L2BBYcbMlwc5RnPgUubBpD6ktlHEWfgC4Sa7Dh9d7EukgtUFq3fPi0z cgaynm+mR2xosOHO8GdvS23U4z5aP6JiHSddKwrKCP2/7YVgYzM/RAo24ZVO2+r2hj1lgAW6+ g9EhzH56tRTguXtyzArAaUCuadT5IBdksIc706VtphTAx4tqHjWHrGFZaVWqF7u5C9WOLRSkQ F0cqYeuclXsDaNs4wOZLCq0VGg9B8nRSPiR+pr2GBjkEKxKvhiS4wpg9P9wQ7daQSU7sLtU5o 7AL4PSTiEgjHXOY2rFL+rDoh058iOizLMNH0Lg2AwAMjhOS06eKwXFIq5r98v9+MCtDYdFCI3 03j1lweyBRxaxB1leU68OkxCiexEpkDQnVxqefTnaqG0usUMsw3ulYKIVFsXZ1iofcq+AnUUv vqNOZQ5FuEqc/2ZoDgcyETPdNpSeJjrYQ0lWaOEmk+7xDMlxM8Jsp/tRttiQXTjRZPKZog7uS yeOVBBEUuV+nPJGBXrEeY+gE12YxjdGt4fBGY+2u3IMUkFoDJ4OwDDGAjIkTiD8uNVhZarUq/ fLAN1PgNVRy2nV72awpfU7wxP/MNfvJhKCiZRAUTocJSVfWR1zaUnjvGHpTMuBq8bOCIrdZgM Z42PC5i/T6MlCB3udMn0kAKHNw6cfXW+s2jlSzQ/kCl9MX77IuvA4Tsjqum5DbxqMoSorwx+k fWAWctuhdPm7lJ4GM Subject: Re: [PHP-DEV] [RFC] switch expression From: cmbecker69@gmx.de ("Christoph M. Becker") On 25.03.2020 at 17:06, Dennis Birkholz wrote: > 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 bool= ean match. >>> >>> This is how Rust does it: >>> >>> ```rust >>> let x =3D match ... { >>> Some(y) if y < 5 =3D> ... >>> } >>> ``` >>> >>> 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 =3D true switch { >>> $x !=3D=3D null && $x < 5 =3D> ... >>> } >>> ``` >> >> 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 t= hen =3D=3D compared against the provided value, that should be "good enoug= h" for most use cases. >> >> The implementation should include some tests to make sure that works pr= operly, but I'm happy with the resulting syntax. >> >> So then the net result is: >> >> $var =3D switch($val) { >> case expr1 =3D> 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 =3D 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? Or maybe even $var =3D $x > 0 ? 1 :($x < 0 ? -1 : 0); Yes, the required parentheses are ugly, but in my opinion, this is still better than a new if or switch(true) expression construct. =2D- Christoph M. Becker