Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:117906 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 59585 invoked from network); 11 Jun 2022 20:15:05 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 11 Jun 2022 20:15:05 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 785581801FD for ; Sat, 11 Jun 2022 15:01:55 -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,HTML_MESSAGE, 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-il1-f176.google.com (mail-il1-f176.google.com [209.85.166.176]) (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 15:01:52 -0700 (PDT) Received: by mail-il1-f176.google.com with SMTP id u2so1793205iln.2 for ; Sat, 11 Jun 2022 15:01:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=WP+769zFmLHvD6HXnc8u84KuXnDWiQpkDYs9V1S7BdI=; b=Mjd0HCyoPqyOt2VIoQ+UxT/2rXiSDsa2BRNutnXbZ1BvrHD+vav7l1fEhKftS2RsWJ kwAnOA1uri/ac1hbonoxy9jjwpIFyYNwxjcEZ+szKRRIHf7bz8i+Tt+vhkJOWvXkGJo0 /FeR9y0CMCt0idHYZtfNbl8sAPgG19uVdGO7a5ICCiw+kfDIbYguChQe4n0vXVqod6us 36S7JlPNPsUZrzr2w9r59F5AdOpI/n2+fkrmYtD9ljRtqa1kwzNJ1r8ylxlC8beVevIB AwKoxYbRvptH15MfPQJshw8srIn5zo/MmIdvB5cyaqGG02SIVV4A8L3JWZ9Oj54dAD3g gsDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=WP+769zFmLHvD6HXnc8u84KuXnDWiQpkDYs9V1S7BdI=; b=u8TnqFOuqXAD3NB3JzssKI8x2t8Cf2TLcWxy4Ey36V3eklkhu/dHFGGdJtrdYhZFkt Sp9n+bEhO4j9MoHJXlL0CJTsEZsUVkbFXE9PZA/Oor/C5yiujhN0szOL7rCJyqYDJgR5 17RHFsmooJGAz25QB9z4ITgLFGR0Uh5E5asUS5zKTzvnigvM/IFA1jD8TZy/DJtzHpkW QrmGFV2J15BwDS9oLs7R/zsW+LpPX53E4xhnQyvc5DMdxXeC05+uTl8HQLnWg3PLnJJk iVZpmL6LrZ7iQ9ozu02h0To51TzYA3n5P/1BBSA5H4Nqh8GqYFDwXKNZklZ/5Dbw6euL s0vA== X-Gm-Message-State: AOAM532ZXacm5mt71Lq+WvOIYkMdpjZlJmBdnHy5xkee/SjSljSTA73b 5HzC3o/YR3bGfRxqK8EZs71eBt+her/wHHKfF5Q= X-Google-Smtp-Source: ABdhPJz/0Rlv0G6mrOTcvk0kRQMTChhgwunONplkSFQsDo7oF7+oWHivibgexIuB+XwW0IJQBdz0Z/k/PTHuwCiX3Yc= X-Received: by 2002:a05:6e02:1849:b0:2d3:f382:bb30 with SMTP id b9-20020a056e02184900b002d3f382bb30mr27392075ilv.144.1654984911548; Sat, 11 Jun 2022 15:01:51 -0700 (PDT) MIME-Version: 1.0 References: <2b35605f-8da8-46b1-aec3-00bd1bfe47fd@www.fastmail.com> <8310f3fd-0011-970e-5379-b2b6e03942b2@gmail.com> In-Reply-To: <8310f3fd-0011-970e-5379-b2b6e03942b2@gmail.com> Date: Sun, 12 Jun 2022 00:01:15 +0200 Message-ID: To: Rowan Tommins Cc: PHP internals Content-Type: multipart/alternative; boundary="000000000000c2f67505e13333aa" Subject: Re: [PHP-DEV] [RFC] Short Closures 2, aka auto-capture take 3 From: deleugyn@gmail.com (Deleu) --000000000000c2f67505e13333aa Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Sat, Jun 11, 2022 at 11:14 PM Rowan Tommins wrote: > 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 compactnes= s > 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 > > > 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. > The RFC does mention that the existing Anonymous Function Syntax remains untouched and will not be deprecated. Whether the new syntax is better for nearly all closures will be a personal choice. If the new syntax doesn't suit, say, a middleware handler, then we still can: - reach for the old syntax - use invocable classes - call another method or function which creates a brand new scope and then returns a function/callable. > > 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() =3D> $x;" > The previous discussions talked about use(*) or use(...) and most people I know that would love this RFC to pass would also dislike that alternative. It does not have the greatest asset for short closure: aesthetics. Maybe my personal bubble is not statistically relevant, but this is where PHP Internals is lacking on surveying actual users of the language to help on such matters. All I can say is that use(*) is not a replacement for the RFC= . > > 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. > Any attempt to make it explicit defeats the purpose of the RFC. The auto-capturing means we don't have to write awkward code to access variables. The only way we have to avoid awkward syntax (such as use ($var1, $var2)) is to declare an entire new invocable class and send the parameters via the constructor. When many variables are involved, that may still be a great option, but doing that just for 1 variable and 2 lines is quite... sad. When I think of new accessors for this particular case, they would either be innovative or verbose. If they are verbose, we already have a syntax for that. If they are innovative, it would be an awkward out-of-place situation that doesn't happen elsewhere in the language. Or I lack the imagination to see a different result. Ultimately, I see fn() as "an opt-in to not create a separate scope for a function". PHP has several language constructs that may or may not create a separate scope. Delimite Scope: function, method, class, procedural file Shared scope: if, for, foreach, include, require and fn > Regards, > > -- > Rowan Tommins > [IMSoP] > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php > > --=20 Marco Aur=C3=A9lio Deleu --000000000000c2f67505e13333aa--