Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:117905 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 55844 invoked from network); 11 Jun 2022 19:27:43 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 11 Jun 2022 19:27:43 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 177491801FD for ; Sat, 11 Jun 2022 14:14:32 -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.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,NICE_REPLY_A, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-wm1-f43.google.com (mail-wm1-f43.google.com [209.85.128.43]) (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 ; Sat, 11 Jun 2022 14:14:31 -0700 (PDT) Received: by mail-wm1-f43.google.com with SMTP id i64-20020a1c3b43000000b0039c6fd897b4so437769wma.4 for ; Sat, 11 Jun 2022 14:14:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:date:mime-version:user-agent:subject:content-language:to :references:from:in-reply-to:content-transfer-encoding; bh=Ahdvenq9MwHhhNPa3WUBWj4ipVitizdZhhUsXc+fVcU=; b=OcjVYYtVL1/BHR4DlVpSpwQkDuL6ZdLTHM9Bs4obAuGYrU6g1/2UJWVtyelqyD/y5+ wL+gtMjiyrjD4vlwRimdUNIDA8HTe3sJbQbKynEnHPevInH4qQgQ2sAO+C/RYwRnfOoo 4pKum2YKW4kNiUGsdccKxd/Eop+IAOANEQEcsrxFyueu0DVL792XBaoE/lamFp1FamAk nvGiUKeOcQwUipxs8m7SMiV0OXzSCY8LDzMFABOPXuVScG9BWo7IUeNZaMKVq9vx3F8C Lw8ejPc+9YZqT4WKd5e70kBp9VKUMNGMsvZHHkn+GtfH8I6g3kwdl+8gwP3NH+rQrdDA dbuQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:references:from:in-reply-to :content-transfer-encoding; bh=Ahdvenq9MwHhhNPa3WUBWj4ipVitizdZhhUsXc+fVcU=; b=dTJP+Fys5YAqfyp47NK3KW6EKBnVhQw/+geT20DAWaoE2W3+YkTt+ZEx5xMlGQx9AQ HHStU7EZyjD05h7lh5bwKbKG8V1IMiueu1j/d7h/2brHFOSzvFevDM3tAOW8LN7s7VQO fffp7cEa9GK75yijLlVRxxiz/a4gSb1ukE/c0LOPN6YZidQkJiVdXK76t5qDsfQJWqvT xZwS/wB4aS2wEIl5nD19155RTWybR7SRjk6DuV+jqpmpomX1rO47io7gO0HKPkDbK9FL oALHmUDIlVXG5lZcl3WrHUAQdPYQG17svfWrSmqF9VfcaBMtaS6mKdUmqXkX6gZdhERi W8hg== X-Gm-Message-State: AOAM531ggUqITLbzBaN5meVfBqna+YI4z/xuc0ndTg3wZL4qKBclfbj0 3HUK56c849CUb/YQI2ot66NOMi9jrFA= X-Google-Smtp-Source: ABdhPJyU8pxV1eGY8CPdT7p2teqNUp271ZL/Y6MQ4exAkfrFzFM31Ijyx+Nw/hWOD21kPfiihkNSSQ== X-Received: by 2002:a1c:7206:0:b0:39c:4d16:683f with SMTP id n6-20020a1c7206000000b0039c4d16683fmr6253956wmc.197.1654982070423; Sat, 11 Jun 2022 14:14:30 -0700 (PDT) Received: from [192.168.0.22] (cpc104104-brig22-2-0-cust548.3-3.cable.virginm.net. [82.10.58.37]) by smtp.googlemail.com with ESMTPSA id q67-20020a1c4346000000b0039751bb8c62sm7463599wma.24.2022.06.11.14.14.28 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 11 Jun 2022 14:14:29 -0700 (PDT) Message-ID: <8310f3fd-0011-970e-5379-b2b6e03942b2@gmail.com> Date: Sat, 11 Jun 2022 22:14:28 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.10.0 Content-Language: en-GB To: internals@lists.php.net References: <2b35605f-8da8-46b1-aec3-00bd1bfe47fd@www.fastmail.com> In-Reply-To: <2b35605f-8da8-46b1-aec3-00bd1bfe47fd@www.fastmail.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] [RFC] Short Closures 2, aka auto-capture take 3 From: rowan.collins@gmail.com (Rowan Tommins) On 09/06/2022 17:34, Larry Garfield wrote: > Last year, Nuno Maduro and I put together an RFC for combining the multi-line capabilities of long-closures with the auto-capture compactness of short-closures ... Arnaud Le Blanc has now picked up the flag with an improved implementation ... The RFC has therefore been overhauled accordingly and is now ready for consideration. > > https://wiki.php.net/rfc/auto-capture-closure First of all, thanks to all three of you for the work on this. Although I'm not quite convinced yet, I know a lot of people have expressed desire for this feature over the years. My main concern is summed up accidentally by your choice of subject line for this thread: is the proposal to add *short closure syntax* or is it to add *auto-capturing closures*? They may sound like the same thing, but to me "short closure syntax" (and a lot of the current RFC) implies that the new syntax is better for nearly all closures, and that once it is introduced, the old syntax would only really be there for compatibility - similar to how the [] syntax replaces array() and list(). If that is the aim, it's not enough to assert that "the majority" of closures are very short; the syntax should stand up even when used for, say, a middleware handler in a micro-framework. As such, I think we need additional features to opt back out of capturing, and explicitly mark function- or block-scoped variables. On the other hand, "auto-capturing" could be seen as a feature in its own right; something that users will opt into when it makes sense, while continuing to use explicit capture in others. If that is the aim, the proposed syntax is decidedly sub-optimal: to a new user, there is no obvious reason why "fn" and "function" should imply different semantics, or which one is which. A dedicated syntax such as use(*) or use(...) would be much clearer. We could even separately propose that "fn" and "function" be interchangeable everywhere, allowing combinations such as "fn() use(...) { return $x; }" and "function() => $x;" To go back to the point about variable scope: right now, if you're in a function, all variables are scoped to that function. With a tiny handful of exceptions (e.g. superglobals), access to variables from any other scope is always explicit - via parameters, "global", "use", "$this", and so on. If we think that should change, we should make that decision explicitly, not treat it as a side-effect of syntax. I don't find the comparison to a foreach loop very convincing. Loops are still only accessing variables while the function is running, not saving them to be used at some indeterminate later time. And users don't "learn to recognize" that a loop doesn't hide all variables from the parent scope; it would be very peculiar if it did. This is also where comparison to other languages falls down: most languages which capture implicitly for closures also merge scopes implicitly at other times - e.g. global variables in functions; instance properties in methods; or nested block scopes. Generally they also have a way to opt out of those, and mark a variable as local to a function or block; PHP does not, because it has always required an opt *in*. Which leads me back to my constructive suggestion: let's introduce a block scoping syntax (e.g. "let $foo;") as a useful feature in its own right, before we introduce short closures. As proposed, users will need to have some idea of what "live variable analysis" means, or add dummy assignments, if they want to be sure a variable is actually local. With a block scoping keyword, they can mark local variables explicitly, as they would in other languages. Regards, -- Rowan Tommins [IMSoP]