Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:113808 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 77381 invoked from network); 27 Mar 2021 00:09:28 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 27 Mar 2021 00:09:28 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 006261804C0 for ; Fri, 26 Mar 2021 17:05:51 -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=-1.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,FREEMAIL_REPLY, HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-ot1-f54.google.com (mail-ot1-f54.google.com [209.85.210.54]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Fri, 26 Mar 2021 17:05:50 -0700 (PDT) Received: by mail-ot1-f54.google.com with SMTP id w21-20020a9d63950000b02901ce7b8c45b4so6821136otk.5 for ; Fri, 26 Mar 2021 17:05:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=9/uGA2P6co10ufvyWmTrKqs2CBc7aKEZDpHCt3V1cm4=; b=RGpjh0AJNXe8oA3DIIAYUknF/S5lY81YHoN9gWLFuEaNZZB1dis0cDfPdcnV+SFQ4i VXxVi5+ymylOjXMNSl3LU/jOUrfOQRzKe49KDsBp7LA9DsE6gPD9WF2tcffm3Ndp++Xe 9xEfYLRmgn/EzEuxNTbrczsXuz0+31qyKvOFmD7SodZCm3muASvz3DEp2NiNmOt1VFYW EFB+Z++tZ6YXT7Dt4d9YWVquTiAikGtHh3HfMzlTiOfcZ04Tx3H2m1bUAtS0a9GoVF+t WFEIeUAA5nPCiAxBq1oWzLDVM1SRxweFQa1dbPNv/6t/HZTX9EzzbfE7viPLJwh97yAn HwjA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=9/uGA2P6co10ufvyWmTrKqs2CBc7aKEZDpHCt3V1cm4=; b=jSUu0zJBkO9nPBOLYeSDrFL8CnEwHgwe2r8Gr6yFUz5FYCpvPqfcnbuUIWnkBWbxOy NoeABp4wNLT+AZCMjwXuIkzprDjoaeSgoS1ckFQ4PsTaf92XSFG6tuO07Wte9+TF7vVS dNPBYSqu0aZZWFSt4DrTt5B1sU/rXcnMFw9wPbIqOnOaByiEfR0CgpQzZCeKec+As2st xLfzc9pnrtUQ7V7NYrYeRWimbKCSgL+1fguUYf1Y1RpnISq06jjGlfMGCxB/WsZKKdOE /17T1eWK6IA078G+1bdlH3fMDzrIyaNDzcG1/9QcD2q8oWbo+ZHYezZLKGMPlk835HZj gLCg== X-Gm-Message-State: AOAM532abYgaHC5pFMSqhwAKPrGOIxMlP51N0fZm+ar6FhT2sKa8svh8 KxbmKJbo+4XvovjpeUnZSNfWsiZUYslJVT0bUUTOAnaH+aHBU/5Q X-Google-Smtp-Source: ABdhPJxdsiIBtWF3tJSKkuqzUe8I2tYcl7APkU2vgjRUf2XWLOyg3Parn0v01Vrinim9jgPoOC/ou+vpgp4pJgqbz7Y= X-Received: by 2002:a9d:1ca1:: with SMTP id l33mr13134370ota.368.1616803549540; Fri, 26 Mar 2021 17:05:49 -0700 (PDT) MIME-Version: 1.0 References: <88c9eb5f-f80c-4869-b7f8-1b58b9e2eaa3@www.fastmail.com> <4DC3B66E-A91A-4AA9-8872-8EE9DE92C2D4@cschneid.com> <8c72c162-83c0-7c7f-2fa7-4fbe3fb30a4a@gmail.com> <605bae82.1c69fb81.f49f7.d11eSMTPIN_ADDED_MISSING@mx.google.com> <919e30e7-3e5e-d955-7bb4-1e1b5825cdd1@gmail.com> <635DD146-FC6F-4991-8D2C-5A6B492722D5@newclarity.net> <734f12de-da98-6b76-c2fe-8682f4d177aa@gmail.com> <36E45DD6-E2BD-4801-BAAE-4355C83D1AC3@newclarity.net> <15AE4315-A456-4ED8-990A-49EBD76C5B46@newclarity.net> <5501FE70-FBED-47EB-8010-173644BC064F@newclarity.net> In-Reply-To: Date: Sat, 27 Mar 2021 00:05:38 +0000 Message-ID: To: PHP Internals Cc: rowan.collins@gmail.com Content-Type: multipart/alternative; boundary="0000000000003df5c405be7969ff" Subject: Re: [PHP-DEV] [RFC] Auto-capture multi-line closures and shortfunctions take 2 From: enunomaduro@gmail.com (Nuno Maduro) --0000000000003df5c405be7969ff Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Rowan, I've just added a few more tests with the exact examples you have presented in this mail list: https://github.com/php/php-src/pull/6246/commits/c3a50d671c5d8fa4b775ec67fe= 77d0cbd5cc8030 . On Fri, 26 Mar 2021 at 16:48, Olle H=C3=A4rstedt w= rote: > 2021-03-26 17:11 GMT+01:00, Mike Schinkel : > >> On Mar 26, 2021, at 8:12 AM, Olle H=C3=A4rstedt > >> wrote: > >> > >> 2021-03-26 3:38 GMT+01:00, Mike Schinkel : > >>> > >>> > >>>> On Mar 25, 2021, at 4:09 PM, Olle H=C3=A4rstedt > >>>> wrote: > >>>> > >>>> 2021-03-25 17:49 GMT+01:00, Mike Schinkel : > >>>>>> On Mar 25, 2021, at 11:22 AM, Olle H=C3=A4rstedt > > >>>>>> wrote: > >>>>>> > >>>>>> 2021-03-25 16:02 GMT+01:00, Mike Schinkel : > >>>>>>> > >>>>>>> > >>>>>>>> On Mar 25, 2021, at 10:41 AM, Rowan Tommins > >>>>>>>> > >>>>>>>> wrote: > >>>>>>>> > >>>>>>>> On 25/03/2021 12:31, Nuno Maduro wrote: > >>>>>>>> > >>>>>>>>> The reason why we believe the vast majority of PHP Developers a= re > >>>>>>>>> going > >>>>>>>>> to > >>>>>>>>> appreciate this RFC is because multi-line short closures (aka > >>>>>>>>> Auto-capturing multi-statement closures) *are more simple, > shorter > >>>>>>>>> to > >>>>>>>>> write, and prettier to read *=E2=80=94 and the community love t= hese > >>>>>>>>> changes > >>>>>>>>> as > >>>>>>>>> proven on "property promotions", "one-line short closures", etc= . > >>>>>>>> > >>>>>>>> > >>>>>>>> My main point was that the RFC needs to spell out this argument, > >>>>>>>> rather > >>>>>>>> than taking it for granted that everyone agrees on "those > >>>>>>>> situations > >>>>>>>> where > >>>>>>>> that is warranted". > >>>>>>>> > >>>>>>>> Most of the current text should be summarised in a "syntax > choices" > >>>>>>>> section somewhere near the end. I would like to see much more > space > >>>>>>>> devoted to: > >>>>>>>> > >>>>>>>> * Why we need this feature. What has changed since it was left o= ut > >>>>>>>> of > >>>>>>>> the > >>>>>>>> arrow functions RFC? What problems is it addressing? Why do you > >>>>>>>> think > >>>>>>>> it > >>>>>>>> is the best approach to those problems? > >>>>>>>> * The exact semantics proposed: How will the variables to be > >>>>>>>> captured > >>>>>>>> be > >>>>>>>> determined? Will it distinguish variables which are written befo= re > >>>>>>>> they're > >>>>>>>> read, and if so how is that defined? Can auto-capturing closures > be > >>>>>>>> nested, i.e. will "fn() { return fn() { echo $a; } }" capture $a > >>>>>>>> from > >>>>>>>> the > >>>>>>>> outermost scope? And so on... > >>>>>>>> > >>>>>>>> > >>>>>>>>> Besides, one advantage of this RFC is that it is consistent wit= h > >>>>>>>>> the > >>>>>>>>> current syntax of the language and with the short-functions > >>>>>>>>> RFC[2]. > >>>>>>>>> For > >>>>>>>>> example, by proposing that "fn" keyword indicates a function wi= ll > >>>>>>>>> auto-capture variables, by value. > >>>>>>>> > >>>>>>>> > >>>>>>>> While it's a cute rationalisation, there's no intuitive reason w= hy > >>>>>>>> "fn" > >>>>>>>> should have that meaning; we could pick any aspect of the curren= t > >>>>>>>> arrow > >>>>>>>> function syntax and say "the 'fn' keyword means that". > >>>>>>>> > >>>>>>>> > >>>>>>>> > >>>>>>>>> On the other hand "use (*)" has no usages / or current meaning = in > >>>>>>>>> the > >>>>>>>>> language. > >>>>>>>> > >>>>>>>> > >>>>>>>> This is a straw man argument. I could equally say that "fn() { } > >>>>>>>> has > >>>>>>>> no > >>>>>>>> usages or current meaning in the language" - of course it doesn'= t, > >>>>>>>> we > >>>>>>>> haven't added it yet! > >>>>>>>> > >>>>>>>> The "function use() {}" part of "function use(*) {}" has a > >>>>>>>> well-established meaning, and "*" to mean "everything" is a > >>>>>>>> notation > >>>>>>>> developers are likely to be very familiar with. > >>>>>>>> > >>>>>>>> The two disadvantages I see with using "fn" as proposed are: > >>>>>>>> > >>>>>>>> * Because it's shorter, people will decide it's the "better" > >>>>>>>> version, > >>>>>>>> when > >>>>>>>> they don't actually need any variable capture. An explicit synta= x > >>>>>>>> like > >>>>>>>> "use(*)" instead makes this a deliberate choice. > >>>>>>> > >>>>>>> And yet adding " use (*)" makes the syntax longer, which goes > >>>>>>> against > >>>>>>> one > >>>>>>> of > >>>>>>> the goals many people have for it: to be shorter. > >>>>>> > >>>>>> I don't understand why this is a target in the first place. Shorte= r > >>>>>> does not mean more readable, and readable is more important than > >>>>>> writable. > >>>>> > >>>>> I agree that readable is more important than writable, but shorter > >>>>> also > >>>>> does > >>>>> not necessarily mean it is *less* readable, either. > >>>> > >>>> Sure. The brain removes noise and reads in "symbols" anyway (where > >>>> "fn" or "function" is a symbol of size 1). > >>> > >>> That is actually not exactly true, at least not in all cases. > >>> > >>> When "nction" combined with " use (.....)" adds to line length such > that > >>> a > >>> developer must scroll horizontally to see all the text then it is not > >>> the > >>> same. > >>> > >>> And this is not a hypothetical for those of us who frequently use > >>> vertical > >>> split screen in our editors =E2=80=94 I am constantly battling with l= ines that > >>> are > >>> too long. > >>> > >>> Also when longer lines cause code to wrap on GitHub or in blog posts = or > >>> other places then it is not the same. > >>> > >>>> A more important aspect of readability is the cognitive load on > >>>> short-term memory, or how many "chunks" the programmer has to keep i= n > >>>> memory to understand a piece of code. In this case, I think > >>>> immutability and local scope helps a lot, of which PHP has neither. = Or > >>>> maybe predictability of the scope? All language quirks hurt > >>>> readability. I never had a problem with scope in JS, despite it > >>>> lacking immutability and only recently got proper block scope. > >>> > >>> Given that the RFC prescribes by-value capture and not by-ref capture > how > >>> it > >>> is really even a problem? Or are you arguing that you fear people wi= ll > >>> just > >>> write closures hundreds of lines long? > >>> > >>> Maybe PHP should deprecate functions longer than 50 or 100 lines? > >>> > >>> > >>>> Maybe more important than explicit/implicit capturing of scope is to > >>>> keep your functions short...? In our legacy code-base, we have > >>>> functions > >>>> thousands of lines long. I can see auto-capturing being a problem > >>>> there, but that's because of the technical debt and not the feature > >>>> itself, I guess. Will our juniors realize that, tho? > >>> > >>> Now here is where I think the real problem is, the fact that other > >>> developers write functions thousands of lines long. > >>> > >>> But realistically, legacy code won't be affected as people are rarely > if > >>> ever going to go back to your legacy code and convert a thousand line > >>> function into a closure with auto-capture. > >> > >> No, I'm more concerned that someone will add a closer at the *bottom* > >> of a long-ass function, capturing all variables, and then not have a > >> properly defined lifetime, causing a memory leak or "spooky action at > >> a distance" with object references. In other words, could it be a > >> foot-gun? Maybe far-fetched, I don't know. > > > > Unless I misunderstand the RFC, the closure would only auto-capture the > > variables that are specifically referenced inside the closure, so I don= 't > > see it capturing *all* variables unless the developer intended it to by > > referencing each one of them explicitly. > > > > And since the RFC captures the variables by-value I don't see how > > auto-capture would be any different than using "use (...)," even with > > objects. > > Oh yeah, good point. My argument is moot. > > Olle > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php > > --0000000000003df5c405be7969ff--