Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:109598 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 98693 invoked from network); 12 Apr 2020 20:49:14 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 12 Apr 2020 20:49:14 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 54E321804B7 for ; Sun, 12 Apr 2020 12:18:29 -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=-0.7 required=5.0 tests=BAYES_40,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_LOW,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, SPF_HELO_PASS,SPF_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS11403 66.111.4.0/24 X-Spam-Virus: No X-Envelope-From: Received: from out4-smtp.messagingengine.com (out4-smtp.messagingengine.com [66.111.4.28]) (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 ; Sun, 12 Apr 2020 12:18:28 -0700 (PDT) Received: from compute7.internal (compute7.nyi.internal [10.202.2.47]) by mailout.nyi.internal (Postfix) with ESMTP id BA8215C00AC for ; Sun, 12 Apr 2020 15:18:27 -0400 (EDT) Received: from imap26 ([10.202.2.76]) by compute7.internal (MEProxy); Sun, 12 Apr 2020 15:18:27 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; bh=ay6zzx mfxsim3ntTUU1adkojbKG8BOA2GgI35ZqyLAs=; b=iVqctRDQc3UvwHLuMwW11L mByYQ7FabqU5gATtLl1v8qcwsh+ErkUuwcQztBWacVgZFB/hhItaUyni6RHAPs1A 53Ja+hX5nJv5sqDSnOk+IkB6z7jWaAd0XMTOlU6iPM3ag7XdtWM+lNtyh+nyONTI KuIZzecmrCtn0byvQbok7AHvUbDcYbA0qjAEDcYTP2NeQgxGmGyR7CdWMoqEGX01 p+bEom4v06mxZRwFhxtYdTVAE9iTYQnCP2RCBKsT5TdhMNWImHKk5z7j28ooqz41 xw+9O7jfWTjqGmkLAtM+7OC5Y5L9XRR9/wLeJnuO9683RDe7aTjZcKiBdx3mi3VA == X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduhedrvdejgddufeekucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepofgfggfkjghffffhvffutgesthdtredtreertdenucfhrhhomhepfdfnrghr rhihucfirghrfhhivghlugdfuceolhgrrhhrhiesghgrrhhfihgvlhguthgvtghhrdgtoh hmqeenucffohhmrghinhepphhhphdrnhgvthenucevlhhushhtvghrufhiiigvpedtnecu rfgrrhgrmhepmhgrihhlfhhrohhmpehlrghrrhihsehgrghrfhhivghlughtvggthhdrtg homh X-ME-Proxy: Received: by mailuser.nyi.internal (Postfix, from userid 501) id 698F214200A2; Sun, 12 Apr 2020 15:18:27 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.1.7-1104-g203475c-fmstable-20200408v2 Mime-Version: 1.0 Message-ID: <4d5a347b-8d9d-43fd-b811-3cb6c13ad06a@www.fastmail.com> In-Reply-To: References: Date: Sun, 12 Apr 2020 14:18:07 -0500 To: "php internals" Content-Type: text/plain Subject: Re: [PHP-DEV] [DISCUSSION] Match expression From: larry@garfieldtech.com ("Larry Garfield") On Sat, Apr 11, 2020, at 7:16 PM, Ilija Tovilo wrote: > Hi internals > > I'd like to announce the match expression RFC that replaces the switch > expression RFC I announced a few weeks ago. > New: https://wiki.php.net/rfc/match_expression > Old: https://wiki.php.net/rfc/switch_expression > > Due to the feedback I decided to take the RFC in a slightly different > direction. The main objections were: > > 1. It didn't behave like the traditional switch so it shouldn't use > the switch keyword > 2. It didn't fix type coercion > 3. The RFC was poorly structured > > In hindsight I understand why I had a hard time writing the RFC. I > tried making a case against the switch statement while really not > addressing the switch statement at all. The new RFC proposes a match > expression that fixes all the objections raised against the switch > statement. Additionally, match arms can now contain statement lists > which allows you to use the match expression anywhere the switch > statement would have been used previously. > > While some people have suggested statement lists aren't necessary I > don't think it makes sense to raise objections against the switch > statement without offering an alternative. > > I also experimented with pattern matching but decided against it. The > exact reason is described in the RFC. > > I'm now confident this is the right approach. I hope you will be > happier with this proposal. > > Happy Easter! This is a substantial improvement over the previous RFC, thank you! 1) The use of => is fine with me. I'm not bothered by it, but I'm also open to other options like : if there's a strong consensus otherwise. 2) I am also still against block statements in the right-hand side, largely for the same reasons as Rowan. They're unnecessary and lead to confusion. In my mind, match expressions are more like a multi-branch ternary than a switch statement. When I say unnecessary, I mean that very literally. Because it can take any expression, that means it can simply execute a pre-defined function or anonymous function. function b() { // Something multi-line and complicated. } $c = function ($x) { // Something multi-line and complicated. }; match ($x) { 1 => 'A', 2 => b(), 3 => $c($x), }; That not only alleviates the need to support multi-line blocks, it keeps the match statement itself clearer to understand at-a-glance, and encourages the definition of named, testable, small blocks of code (ie, functions whether anonymous or not), which is a net win on its own. The overall effect is the same. And if you really, really need to inline the block for some reason, this is ugly but syntactically valid: match ($x) { 1 => 'A', 2 => b(), 3 => function() use ($whatever) { // Something multi-line and complicated. }(), }; (Incidentally, the same "factor complex expressions out to a function an everyone is better off" applies to the left hand side, too. That's a good thing.) I would strongly recommend removing the block statement support, as it just muddies the water. 3) I'm fine with match() always being strict comparison, regardless of the type mode. However, it should probably be made more explicit in the RFC that it is unaffected by the type mode. 4) Possible addition: Make the match input optional, defaulting to "true". That is: match (true) { $x < 5 => 'A', $x > 10 => 'B', $x ==7 => 'C', default => 'D', } Can be shortened to: match { $x < 5 => 'A', $x > 10 => 'B', $x ==7 => 'C', default => 'D', } Since it's an expression it still has all the values available from the scope, so it's just as capable and saves a little typing in that case. --Larry Garfield