Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112510 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 70526 invoked from network); 14 Dec 2020 23:59:23 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 14 Dec 2020 23:59:23 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 3B0691804F2 for ; Mon, 14 Dec 2020 15:30:11 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=0.6 required=5.0 tests=BAYES_40,BODY_8BITS, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_LOW,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mout.gmx.net (mout.gmx.net [212.227.15.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Mon, 14 Dec 2020 15:30:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1607988608; bh=PxMeFpn+I9dQ8ddtf4zToHMhM0+uezIoj2UrkqrwnQs=; h=X-UI-Sender-Class:Subject:To:References:From:Date:In-Reply-To; b=GJgOXPj8vPu+sne0j2tKBDm/f9r4W6KW6LrZ1Ii8IQYdaPOQXpN0WGPxQwlC/7QFI pLsDL0MJaanPvhmQJCyL4SR+T3Hry3epXCAHxP0hcF/OEi73nxKWb1g35E8RskERjt Mxs9ytPndE+eWcB10TVBggymGmn5JWeJ90PmagzM= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from [192.168.178.120] ([24.134.51.41]) by mail.gmx.com (mrgmx004 [212.227.17.190]) with ESMTPSA (Nemesis) id 1MeU0k-1kEW0q2YT6-00aReH for ; Tue, 15 Dec 2020 00:30:08 +0100 To: internals@lists.php.net References: Message-ID: <5f138466-1da4-0048-3cba-13c0d2da5732@gmx.net> Date: Tue, 15 Dec 2020 00:30:37 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.5.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: quoted-printable Content-Language: en-US X-Provags-ID: V03:K1:HacP8WF2jBijvTpO54KyHdvcUaQJTgXKrzX7ZPMxonh7FsQpgCH LlG4RFbes2L3oQoOVnaIMkf0Mw0W51W82V7pOiT/NndVJBTDzUTKTH7Bi/bRzO19tFhcuRO c3zABNUMKHlQN0zJSFpxjw4+JiVzx9hF0lEeU+bULoNUnhm7nfU+FURVRuSZLyYdLYuqVs0 vPBk8RuVi5h5Mzsc9S4Pg== X-UI-Out-Filterresults: notjunk:1;V03:K0:aQxnxLx6J2s=:gLrqqTtKAEbdtG//kvdEau HckiqxsAxHYnyWDMCjIyGyWmHWplBmnlBQEpLXBepJkpTHgY/zNGUkxNmgEFuInOXN8ujvo8A c4Y2ADnmFxfVCPCqNlXdYYfPrexuKDo0YMFyZDV1fQH3JqhE/8IXCBuWG5SidJGBbXH3Ii8Sc +QZDFSxLfvwqFgAsFAH6igBZOQ/PaUG9vfxHP/2YoHaACfpssRmUuzpXaAF+qN+GZf/PHu/oc rNOBVy3vA1ScpsUqUWwZlsHF1hBuOEOuDYYe21AIXcEc1JL7KBYNquyudtvOsE5E8mAuGF0n6 67vJV3AEd6KL8Y49IznVYSHzPTLLnqyOFE7h/nLF8/O+N/XFhcunayM4Y3sk3kCHUtUlkccJT VtkX2IKQKptJYD2bbbRKI8WSVM9j/Z+K3wN95CYUJPH8pFn7FwHp5kkj+vywrftKfZaqShgWF 8ZKQiWXma0+RcshBCpWWYwX/1PkymapmovnnBec+jcjmDxGzchipWxA4Tm2b1oF/MvIpyZ2D0 aGrMjaOWsp4AsnV2LE6svWIq9n1zBUpnzNkRsFj8BCQ+WIMs+RdcXEsEQqdxdyNRbhviF3Qvk aaKpJZocHihMoI6R/T6Td/SW0yjtTWoHzNA/gWHtudCvbMObnGUntUP8Z628QXz0sxr4b5qDY oJnK4V8WI54/NuBr+39bFOO4nAMPsVnot38QvitKXQLwO8GR4tuRGKrQE/BnMlwz/Jz9QX/A9 lSZ9+YgCqXhbSFzasMNcV/t1KZY6QKkZcyQCXPz4nbvSYKILcdgZNo05Y1vCC8hmNSAxUEQ+x J/SddyzgkNNn9lsxe9XdGodnhKVWigEcNV28FaNZz3GcUL0dEWrJvmcgc9yvDHyPjeOwn2p+R YC0mhtc/smJiowhPQldA== Subject: Re: [PHP-DEV] [RFC] Short-match From: a.leathley@gmx.net (Andreas Leathley) I checked in my vendor directory for currently used switch(true) usages, there were 96 matches. It is often used when handling an unknown value and needing to display or convert it, as there the structure of a switch is easier to scan compared to other comparisons like ifs (at least it is for me). Compared to match these switch statements are a lot uglier, which shows that there are use cases for it even when you are forced to use switch. This is a good example in my estimation, from the Symfony Console Dumper class (taken from Symfony 5.2), when a value is converted into a string representation: ```php $this->handler =3D function ($var): string { =C2=A0=C2=A0=C2=A0 switch (true) { =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 case null =3D=3D=3D $var: =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 return= 'null'; =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 case true =3D=3D=3D $var: =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 return= 'true'; =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 case false =3D=3D=3D $var: =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 return= 'false'; =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 case \is_string($var): =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 return= '"'.$var.'"'; =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 default: =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 return= rtrim(print_r($var, true)); =C2=A0=C2=A0=C2=A0 } }; ``` With match this becomes much more concise: ```php $this->handler =3D function ($var): string { =C2=A0=C2=A0=C2=A0 return match { =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 null =3D=3D=3D $var =3D> 'null= ', =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 true =3D=3D=3D $var =3D> 'true= ', =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 false =3D=3D=3D $var =3D> 'fal= se', =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 \is_string($var) =3D> '"'.$var= .'"', =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 default =3D> rtrim(print_r($va= r, true)), =C2=A0=C2=A0=C2=A0 }; }; ``` The same with ifs: ```php $this->handler =3D function ($var): string { =C2=A0=C2=A0=C2=A0 if (null =3D=3D=3D $var) { =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 return 'null'; =C2=A0=C2=A0=C2=A0 } =C2=A0=C2=A0=C2=A0 if (true =3D=3D=3D $var) { =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 return 'true'; =C2=A0=C2=A0=C2=A0 } =C2=A0=C2=A0=C2=A0 if (false =3D=3D=3D $var) { =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 return 'false'; =C2=A0=C2=A0=C2=A0 } =C2=A0=C2=A0=C2=A0 if (\is_string($var)) { =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 return '"'.$var.'"'; =C2=A0=C2=A0=C2=A0 } =C2=A0=C2=A0=C2=A0 return rtrim(print_r($var, true)); }; ``` The implied return type for match and the reduced amount of code to scan makes match {} much better in my opinion, with ifs you always have to make sure there isn't additional logic somewhere, and it makes it easy to add more complex code "by accident" compared to match. match (true) would be possible now, but the true makes the code a bit confusing, while without the (true) it reads more like natural language ("match the first possible expression in this list and return a corresponding value").