Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:121048 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 96096 invoked from network); 12 Sep 2023 18:16:45 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 12 Sep 2023 18:16:45 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 337871804B0 for ; Tue, 12 Sep 2023 11:16:44 -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.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_NONE, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS19151 66.111.4.0/24 X-Spam-Virus: No X-Envelope-From: Received: from out2-smtp.messagingengine.com (out2-smtp.messagingengine.com [66.111.4.26]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature ECDSA (P-256) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Tue, 12 Sep 2023 11:16:43 -0700 (PDT) Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id 682A75C006C for ; Tue, 12 Sep 2023 14:16:42 -0400 (EDT) Received: from imap50 ([10.202.2.100]) by compute4.internal (MEProxy); Tue, 12 Sep 2023 14:16:42 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= garfieldtech.com; h=cc:content-type:content-type:date:date:from :from:in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to; s=fm1; t=1694542602; x= 1694629002; bh=MbBM1hmhVE1oQKCe1WUH1zgvtvbhIIUDxY45+HkBEOA=; b=N yspUiuvtkNs+zMsDqDBPgpt9KvUzaosP7NKfC+oa5j3X+XWckguaEEYx9I6pDfmJ wJ3qx0rKLvZNlIs8CDf4foMwUv9iOwZhJhZnSj14EZaFfNREW1Oryr+bPqGry4eN 1U3szLejr3kicc70qqfmz4HS4LUQkoRNAyPBEek4qb0315YVcqnnrMObBmS4nxqj IVJ7JW0SL5ryD7zg/EjT6aFLz02byh2It0E3oE55N2tqup3tuciycU6H7QCcg4J9 DGDkKaXrCTJRI1iRJRTiywaw7mIczsh35BK++Sh7/AjHpAGdKhUjiAB6Qskki4Cj jHeo/jiL0WqRxnQ7XHpYw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:sender:subject :subject:to:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm1; t=1694542602; x=1694629002; bh=MbBM1hmhVE1oQ KCe1WUH1zgvtvbhIIUDxY45+HkBEOA=; b=cNIc2hkBZhUulJ/7R3Mjm7yIJ/lMB oWapLIVN8+8a4w/QtfYVnoGoawIfxs4kwCPwARtNbm2WuXixn32cGv5UgYH+/uqt zdQtf9VevlBTKexUS5aH+3/Haq6QqiWPB+PsLnIBuj8GHCSkM8RaK1RojGhJTMtn XHuKAl/miBzne/+XpHHjf6mc0W1HhkgnhWCpKYGj5RV0wT49Qq2puoHWMeu8xEO2 cZl3YJ/Np6xP+/rw5nmOnBXBL4Vk0Pt92JC+Cx2rRINURneuReiQMb+zZSA2+5Xs lqAyACn+uFFxdktyCWg3z9AI2ZcK7RQrqugfoLnf08TabyTeoCJOEt+vg== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedviedrudeiiedguddvfecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpefofgggkfgjfhffhffvufgtsehttdertderredtnecuhfhrohhmpedfnfgr rhhrhicuifgrrhhfihgvlhgufdcuoehlrghrrhihsehgrghrfhhivghlughtvggthhdrtg homheqnecuggftrfgrthhtvghrnhepgfdvkeegieelfefhkefhteetvdeffeevlefhfeeh ieeijefggeettefghefhgfeunecuffhomhgrihhnpehphhhprdhnvghtpdhruhhsthdqlh grnhhgrdhorhhgnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhf rhhomheplhgrrhhrhiesghgrrhhfihgvlhguthgvtghhrdgtohhm X-ME-Proxy: Feedback-ID: i8414410d:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id DF5441700089; Tue, 12 Sep 2023 14:16:41 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.9.0-alpha0-745-g95dd7bea33-fm-20230905.001-g95dd7bea Mime-Version: 1.0 Message-ID: <4965ef59-1937-44aa-a3ec-f67664420300@app.fastmail.com> In-Reply-To: References: Date: Tue, 12 Sep 2023 18:16:16 +0000 To: "php internals" Content-Type: text/plain Subject: Re: [PHP-DEV] [RFC][Draft] Match block From: larry@garfieldtech.com ("Larry Garfield") On Fri, Sep 8, 2023, at 10:16 PM, Ilija Tovilo wrote: > Hello everyone > > I've been working on match blocks over the last few weeks. > https://wiki.php.net/rfc/match_blocks > > I've already shared it in R11 and got conflicting feedback, which > makes me unsure on how to proceed. We have a few options. > > 1. Add blocks only to match, possibly adding blocks to other > constructs in separate RFCs (the approach of this RFC) > 2. Support block expressions as a language-level concept, analogous to > https://doc.rust-lang.org/reference/expressions/block-expr.html > 3. Do nothing > > The two main complaints/questions I've gotten was whether this > approach is the right one, and whether the syntax can be improved. The > RFC tries to go into detail on explaining the rationale for the chosen > approach. Additionally, it proposes a few alternate syntax options, > although none of them are very satisfactory. > > At this point I'm unsure whether a proposal can satisfy all camps. Let > me know if you have any thoughts/ideas. > > Ilija As we've discussed elsewhere, I favor option 2: Language level concept. I cannot speak to the implementation details, but let me offer two specific bits of justification. 1. The RFC says "For match, whether the block should require returning a value depends on whether the match itself returns a value." - I disagree. Returning a value should always be legal; if the value isn't assigned to a variable it is lost, but oh well, that's how PHP already works. Moreover, if the argument is rather "if match's result is saved, then each branch *must* return a value", well, the same is true for use in many other cases, such as with the null short-hands. 2. Speaking of, this is a pattern I find myself writing all the time: class Foo { private array $things; public function getThing(string $id): ?Thing { $this->things ??= $this->makeThingTable(); return $this->things[$id] ?? null; } private function makeThingTable(): array { // Something complex here. } } Allowing multi-line expressions on ??= would allow that to be collapsed to a single function, without the need to come up with a name for makeThingTable(), or splitting logic between two methods, etc. I believe that is a valid and useful use case for multi-line expressions outside of match() that can/should be supported. (One could argue that it's possible to just use a self-executing closure, which is true, but is also verbose and ugly and requires manual capture. That same option is also available for match arms today, with the same problem, which is why we're looking for a better solution.) class Foo { private array $things; public function getThing(string $id): ?Thing { $this->things ??= { // Something complex here. }; return $this->things[$id] ?? null; } } I am flexible on the syntax used. I agree that a keyword would be more PHP-ish than <-, but I wouldn't start a riot either way. If just { } makes the parsing hard, I have in the past suggested {{ }} to denote a multi-line expression, which should be sufficiently parser-unique. > We could settle for a solution that works for all cases, returning null by default. Whether this solution is acceptable is likely a matter of taste. That would taste just fine to me, frankly, and I'm OK with the code block shown right after it. --Larry Garfield