Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:117934 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 32002 invoked from network); 13 Jun 2022 12:05:50 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 13 Jun 2022 12:05:50 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 7482B180384 for ; Mon, 13 Jun 2022 06:53:05 -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 RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Mon, 13 Jun 2022 06:53:04 -0700 (PDT) Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.nyi.internal (Postfix) with ESMTP id 9922C5C0217 for ; Mon, 13 Jun 2022 09:53:04 -0400 (EDT) Received: from imap52 ([10.202.2.102]) by compute1.internal (MEProxy); Mon, 13 Jun 2022 09:53:04 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= garfieldtech.com; h=cc: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=fm2; t=1655128384; x= 1655214784; bh=sU6mUubxADnJOMrLG49ecZeawL9WPec06WDuiZiTc/M=; b=P cDlf+kEJCm+9CLMDHrrHKGfrITUavzpS7WEszIFD4OOxELE9gpt605xPzs6qU+FT M8t+17/ovFWYOisc2bRLA3wOTXEQ4fgkAzKuAPF+rhG9i4bdbHE5InaXkp8FhlgE j6LVa4YOEgFdA624B99jx81e5cz1pUaQaP8tf2+SWK2aEjwJU8/d3z6/SN0uPhbx nLlB5nOwjIvDqwxGjxRARg8rEnhBrdkxy5BFSy7rxq1tpOOstnsiQEXKYgXeYCfd f3CriF3bpxOp12nzgnv/1+Zts/oHiGvY6DgFcsgamGAnLeXtzyG8KfEOHqsEGf29 aB+fCtnVwaTQk62HFXC7g== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc: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= fm2; t=1655128384; x=1655214784; bh=sU6mUubxADnJOMrLG49ecZeawL9W Pec06WDuiZiTc/M=; b=k/p1hF7qJoigh5cas/5UuqKVZwA5LtVeCmu3jcnc+H1w V0Qhkfk9S4wLmhiX9a8eYzyMFl4clfhPiamO0iksqhBSEWxfGRq5GXHC9qedgj3e r75qJsB+hrb7gIPtwrF+MN10YhcwYYCYqEQvr1obWt8+4/k/wzpYH24pxBQYgvkD 9hi7IudJKfb8aUhJdDsL48CS82mxcND39UpvqrNVWux9PJ+axkLaPbAjbIdCpk3d oKnXzJPQK5zF2L+cRi25EfP/CrbK5fir0FBbVv3RbBYDSpcAAOX+dq0MIm9wBQl+ mJ9wVKbfUSI3HffxWM31OD+yUuDEpaScHZhupwPdnA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvfedruddujedgieekucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepofgfggfkjghffffhvffutgesthdtredtreertdenucfhrhhomhepfdfnrghr rhihucfirghrfhhivghlugdfuceolhgrrhhrhiesghgrrhhfihgvlhguthgvtghhrdgtoh hmqeenucggtffrrghtthgvrhhnpeeglefgkeduiedvvdetffeujefftdfhjeeiveehgfff keduveektddvledvvdfffeenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmh grihhlfhhrohhmpehlrghrrhihsehgrghrfhhivghlughtvggthhdrtghomh X-ME-Proxy: Feedback-ID: i8414410d:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id 41D09C6008A; Mon, 13 Jun 2022 09:53:04 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.7.0-alpha0-692-gb287c361f5-fm-20220603.003-gb287c361 Mime-Version: 1.0 Message-ID: <39470114-2c35-4c71-b8ef-c8d3ce3c3555@www.fastmail.com> In-Reply-To: <8422e11c-4a3f-9f99-56ce-884491dfe08e@gmail.com> References: <2b35605f-8da8-46b1-aec3-00bd1bfe47fd@www.fastmail.com> <8310f3fd-0011-970e-5379-b2b6e03942b2@gmail.com> <2347345.PIDvDuAF1L@arnaud-t490> <8422e11c-4a3f-9f99-56ce-884491dfe08e@gmail.com> Date: Mon, 13 Jun 2022 08:52:37 -0500 To: "php internals" Content-Type: text/plain Subject: Re: [PHP-DEV] [RFC] Short Closures 2, aka auto-capture take 3 From: larry@garfieldtech.com ("Larry Garfield") On Mon, Jun 13, 2022, at 8:36 AM, Rowan Tommins wrote: > On 13/06/2022 13:57, Arnaud Le Blanc wrote: >> The proposal is to extend the Arrow Functions syntax so that it allows >> multiple statements. > > > That's one perspective. The other perspective is that the proposal is to > extend closure syntax to support automatic capture. As noted before, this is a distinction without a difference. The proposed syntax brings in one aspect of short-closures and one aspect of long-closures. Which you consider it "coming from" as a starting point is, in practice, irrelevant. > By the way, the current RFC implies you could write this: > > fn() use (&$myRef, $a) { $myRef = $a * $b; } > > It's clear that $myRef is captured by reference, and $a by value; but > what about $b? Is it local to the closure as it would be in a "long" > closure, or implicitly captured by value as it would be with no "use" > statement? > > It might be best for such mixtures to raise an error. The RFC already covers that. $b will be auto-captured by value from scope if it exists. See the "Explicit capture" section and its example. >> Auto-capture in PHP is by-value. This makes this impossible. It also makes >> explicit declarations non-necessary and much less useful. > >> Live-variable analysis is mentioned in as part of implementation details. It >> should not be necessary to understand these details to understand the behavior >> of auto-capture. > > > As noted in my other e-mail, by-value capture can still have side > effects, so users may still want to ensure that their code is free of > such side effects. > > Currently, the only way to do so is to understand the "implementation > details" of which variables will be captured, and perhaps add dummy > statements like "$foo = null;" or "unset($foo);" to make sure of it. There's two different issues you're raising here that almost seem to be contradictory. 1. Auto-capture could still over-capture without people realizing it. Whether this is actually an issue in practice (or would be) is hard to say with certainty; I'm not sure if it's possible to make an educated guess based on a top-1000 analysis, so we're all trying to predict the future. Note, however, that this risk is already present for short-closures, as the capture logic is the same. Arguably it's less of an issue only because short-closures are, well, short, so less likely to reuse variables unintentionally. However, based on my top-1000 survey, even today the vast majority of long-closures are only 2-4 lines long. I don't believe that makes it 2-4 times more likely, as it's still trivial for a developer to look at a 2 line closure and say "oh, I'm reusing that variable name, maybe that's not as clear as it could be." 2. The syntactic indicator that "auto capture will happen". The RFC says "fn". You're recommending "use(*)". However, changing the indicator syntax would do nothing to improve point 1. It's just a longer indicator to use the same logic, especially as it would also require the full "function" word. (Making fn and function synonyms sounds like it would have a lot more knock-on effects that feel very out of scope at present.) --Larry Garfield