Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:118140 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 98750 invoked from network); 30 Jun 2022 14:13:35 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 30 Jun 2022 14:13:35 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id B6C971804F8 for ; Thu, 30 Jun 2022 09:05:07 -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, 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-yb1-f175.google.com (mail-yb1-f175.google.com [209.85.219.175]) (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 ; Thu, 30 Jun 2022 09:05:07 -0700 (PDT) Received: by mail-yb1-f175.google.com with SMTP id q132so34544992ybg.10 for ; Thu, 30 Jun 2022 09:05:07 -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=paSsCEfP5L62YzckjpEwWVtybD147Db9fY7PYBBwIm8=; b=jfSxftd2gRzBMqC7SDFol4ywZpCc4uehzM7h8eb+5Ot6N5YQcRSiZ5m5wiPCb1CH4L X4PB8Lt5bWkWtieJjwsC4CdWYL5CwgDF+iP2SJL6WnHiMmRTAQZjSOkbs/p1hxF5MK4o sImEYRXjy4KX6a59xdZiD6zwggQLBpvPMLvNLLj4PupOpmfNbZVU9A8BP8n4rcdqDkY9 xKgfUhzEdw6C4ZxeBDbI6xX7fjhD0YlO6l51qH8l/MXxBg0kH77M0gPyt33KlFgVwqos RIEK/KvB0CTcVXX0LtU+vdg0cwFVJd+BOhPuBxbkfymESbx8zt9aYyW+xKNqUTiEz9h1 twtg== 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=paSsCEfP5L62YzckjpEwWVtybD147Db9fY7PYBBwIm8=; b=GUFzVytR9SnsDm8XvQFyqexBYrRwU9OKTp63jOjWOhZtHbeQcXOiawsSTLLzbB6urW HWKn32rl6/W0yOpkpK4cXBc7WvLYacaNIR1D4ua73XVcUn5OGM7QJmeFg987dX/DpPvC qZ85TFTumVfAbVEKw6sROOVLrz05GFwY8CqsoIA51i/pOe/Ju57pWbHClH8y1pZk/x1V hH2BBOOcSsrWPro0x9SLat5PY5kmbEOxaW+1S130DF+Pj2zamQ5WL7+Aqorm+99zAxLf /nmNQTwv8MJ7kIt6B3rbewIsD2Xo9QjpCAou8dhrHW185CR1OP1LxZFBWmYYL6jCcv6j E3Qw== X-Gm-Message-State: AJIora/xHxRicloZcwfsaZqoSlbycWgseVSUU3QU5efhaguRvY41+agW rrNu2wGXZO9dlmyDDeGcsCyz//ItmgzUML48Tls= X-Google-Smtp-Source: AGRyM1sjqdebYkiFLia6wb4nUSj2eNGeJwl5c1Ndb7gUOZW8drbDW7j8foWSWzFPTGwdCXNkUXhUAYMyf96emWn/1zw= X-Received: by 2002:a25:3383:0:b0:66b:6205:1583 with SMTP id z125-20020a253383000000b0066b62051583mr9973622ybz.387.1656605106629; Thu, 30 Jun 2022 09:05:06 -0700 (PDT) MIME-Version: 1.0 References: <2b35605f-8da8-46b1-aec3-00bd1bfe47fd@www.fastmail.com> <84eb5551-cfaa-42e6-9a74-ae229d5e269c@www.fastmail.com> <50894bde-74cc-b5a4-4bb7-ba0e682f952c@gmail.com> <2dcd5fe0-7284-138b-990c-512966141951@gmail.com> In-Reply-To: Date: Thu, 30 Jun 2022 18:04:53 +0200 Message-ID: To: Guilliam Xavier Cc: internals Content-Type: text/plain; charset="UTF-8" Subject: Re: [PHP-DEV] [RFC] Short Closures 2, aka auto-capture take 3 From: landers.robert@gmail.com (Robert Landers) On Thu, Jun 30, 2022 at 5:47 PM Guilliam Xavier wrote: > > On Thu, Jun 30, 2022 at 3:26 PM Dan Ackroyd wrote: > > > > Hi Rowan, > > > > Rowan wrote: > > > For that to work, it would require the variable to be captured by > > > reference, not value. > > > ... > > > The only way for it to work would be using capture by reference (not > > > supported by the proposed short syntax): > > > > I wrote about this before. Some of the words in the RFC are, in my > > opinion, quite inaccurate: > > > > Danack wrote in https://news-web.php.net/php.internals/117938 : > > > Those statements are true for scalar values. They are not true for objects: > > But the RFC has been updated since (notably the DateTime example); do > you find the current wording still inaccurate? > > > With automatic capturing of variables, for the code example I gave the > > user would want the variable to be captured, and to them it looks like > > it should be, but because of an optimization it is not. > > Am I missing something here? To me, it has been explained (and shown) > by Rowan (and me) that the code example you gave would *not* work as > expected *even without the optimization* (for it to work it would need > to either capture by *reference*, or use e.g. > `$some_resource->close();` [or `close($some_resource);`] instead of a > destructor); but maybe we don't "expect" the same behavior in the > first place? > > > When the code doesn't work as they expect it to, the programmer is > > likely to add a var_dump to try to see what is happening. Which makes > > it look like their code 'should' work, as their resource object is > > still alive. > > This indeed seems a valid point (that adding a > `var_dump($some_resource);` before the `$some_resource = null;` > changes it from "not captured" to "captured", with an effect on its > lifetime). But are there "real" cases where it would *actually* > matter? > > > > In fact, the "optimisation" is in my opinion a critical part of the > > > semantics, to avoid the opposite problem: > > > > As I said, I think that problem is a lot easier to explain "either use > > long closures or change your variable name if you don't want it > > captured." than trying to explain "yes, the variable is referenced > > inside the closure, but it's not captured because you aren't reading > > from it". > > Same as above. > > > On Thu, Jun 30, 2022 at 4:19 PM Robert Landers wrote: > > > > Rowan wrote: > > > No, the captured value is tied to the lifetime of the closure itself, > > not the variable inside the closure. > > > > With the "optimization," it won't be captured at all by the closure, > > possibly causing some resources to go out of scope early. > > And it has been explained that conversely, capturing it would possible > cause some resources to "remain in scope" late. > > > Are > > optimizations going to be applied to single-line arrow functions (I > > didn't see that in the RFC, but I admittedly didn't look that hard and > > I vaguely remember reading something about it in one of these > > threads)? > > Seems so: https://github.com/php/php-src/pull/8330/files#diff-85701127596aca0e597bd7961b5d59cdde4f6bb3e2a109a22be859ab7568b4d2R7318-R7320 > > > If so, it will probably change some behaviors in existing > > applications if they were relying on it. Perhaps static analysis tools > > can detect this and inform the developer. > > Here too, do you have a "real" case where it would *actually* matter? > > > Here's Dan's code: https://3v4l.org/99XUN#v8.1.7 that he just sent, > > modified to not capture the $some_resource and you can see that it is > > indeed released earlier than if it were captured. > > And here it is "un-modified": https://3v4l.org/gZai2 where you see > that calling $fn() (which internally nullifies *its local copy of* > $some_resource) does *not* release; is it really what you expect? are > you creating the closure only to extend the lifetime of > $some_resource? > > > Regards, > > -- > Guilliam Xavier > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php > > And here it is "un-modified": https://3v4l.org/gZai2 where you see that calling $fn() (which internally nullifies *its local copy of* $some_resource) does *not* release; is it really what you expect? are you creating the closure only to extend the lifetime of $some_resource? Personally, not that I'm aware of, which is the point. This may subtly change code that works just fine today and it will be hard to track it down. Though perhaps static analysis/IDE's will help track it down by pointing out automatically captured vs. non-captured variables. Ah, I see Arnaud just confirmed that it won't be applied to existing arrow functions. Perhaps this is a moot point and it will be just another quirk to be aware of when writing PHP. I was just worried about it being applied to any existing code.