Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112512 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 8856 invoked from network); 15 Dec 2020 09:16:53 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 15 Dec 2020 09:16:53 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 203161804E4 for ; Tue, 15 Dec 2020 00:47:47 -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=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,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 mail-lf1-f51.google.com (mail-lf1-f51.google.com [209.85.167.51]) (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 ; Tue, 15 Dec 2020 00:47:46 -0800 (PST) Received: by mail-lf1-f51.google.com with SMTP id s26so2597449lfc.8 for ; Tue, 15 Dec 2020 00:47:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=oDr0j4XScKY0iQ/O/xH/NukynLd728L+p4kY35absx8=; b=IbYDZBW1hmGFDY5cqgWjnb3W/LN3GN8QpQ3TSCvU6NNmrDbTzT6Fm44+QJoIiALKrw dSpfichLUjwhRyVLIFIblxRoEbj4Nart7+tGlv48reR2C2ydUoiUcvbeySsxYDiQ3LQ3 /pF+xguMJV4o6lyJM2wf313/uLLiok8laK3mbQQ2MKIGY0GBEIzzJYDGMy3w9gBNlBDs z5FWl4kUEYgr0rM4plHlUsj0pFgGDuiU5cKLLg2W6gSDlscGjGgC79PdbKz+Kebp33aV aWIBl+ovTZLC4Ma0zRHetM79wBMhA90YGzA+NQHFT4AaXyuFHucFxmgrKC+Kw37bnGno eLbQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=oDr0j4XScKY0iQ/O/xH/NukynLd728L+p4kY35absx8=; b=aE3SaEzqIJcHEbIshIC8nOA4UCQ2XK93W6wyn2yIfKNBQqQAR/1Le6LA5Xqh92hmel JeoJIKA/CIaLoGlVgKdhld7YaUUwEmMjEKTWK0adO4ROg/slvgnd780qUY1olN/UkarL C++JlOgognxke2IqrVzzr6+bdWrf1O/kPrOJsNpQt+b7D/xeVT+g0EXKNP5Tr0fxARYK izHvSks7c+9gRhny5OwXOwv+rKyxXvzNK9a3kkJk6cksQ+ESeo7CxNbFB5bbqx6ijC+0 kKY3aoyE3QblSvxvWyaWotkzUrpG0zZryHPc+ez3oS1E/TyLW22R4PxhtMM/ha6hw8oN 7yAQ== X-Gm-Message-State: AOAM532WOVwvgfwLgqqGT4mGnmGpIQQTDSd8R7rYC3XW+WtdeVnYhlya IpVPFR1lp7hO4JEAFGuULUsMVG6uZ3Mertb18W8= X-Google-Smtp-Source: ABdhPJxiJckl9iKEeang8ey23bjf6lkqRmn6Emq0D5VnbuHQgkJg3GHoWdas+ZHDWDxCKfSuZoxkMrpxChb5Mps14Yg= X-Received: by 2002:ac2:4987:: with SMTP id f7mr10506690lfl.41.1608022064491; Tue, 15 Dec 2020 00:47:44 -0800 (PST) MIME-Version: 1.0 References: <5f138466-1da4-0048-3cba-13c0d2da5732@gmx.net> In-Reply-To: <5f138466-1da4-0048-3cba-13c0d2da5732@gmx.net> Date: Tue, 15 Dec 2020 09:47:31 +0100 Message-ID: To: Andreas Leathley Cc: PHP internals Content-Type: multipart/alternative; boundary="000000000000f1ef6405b67ccf91" Subject: Re: [PHP-DEV] [RFC] Short-match From: olleharstedt@gmail.com (=?UTF-8?Q?Olle_H=C3=A4rstedt?=) --000000000000f1ef6405b67ccf91 Content-Type: text/plain; charset="UTF-8" On Tue, 15 Dec 2020, 00:30 Andreas Leathley, wrote: > 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 = function ($var): string { > switch (true) { > case null === $var: > return 'null'; > case true === $var: > return 'true'; > case false === $var: > return 'false'; > case \is_string($var): > return '"'.$var.'"'; > default: > return rtrim(print_r($var, true)); > } > }; > ``` > > With match this becomes much more concise: > > ```php > $this->handler = function ($var): string { > return match { > null === $var => 'null', > true === $var => 'true', > false === $var => 'false', > \is_string($var) => '"'.$var.'"', > default => rtrim(print_r($var, true)), > }; > }; > ``` > > The same with ifs: > > ```php > $this->handler = function ($var): string { > if (null === $var) { > return 'null'; > } > if (true === $var) { > return 'true'; > } > if (false === $var) { > return 'false'; > } > if (\is_string($var)) { > return '"'.$var.'"'; > } > > 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"). > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php In OCaml they actually use `function` as a shorthand for lambda + match (on the implied first argument). `fun` is the lambda keyword (and also `let`). > > --000000000000f1ef6405b67ccf91--