Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:118134 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 84590 invoked from network); 30 Jun 2022 12:27:27 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 30 Jun 2022 12:27:27 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 5AC3E1804D4 for ; Thu, 30 Jun 2022 07:18:58 -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-f180.google.com (mail-yb1-f180.google.com [209.85.219.180]) (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 07:18:57 -0700 (PDT) Received: by mail-yb1-f180.google.com with SMTP id v185so24493631ybe.8 for ; Thu, 30 Jun 2022 07:18:57 -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=edQyUB3GEvh33HPxtgbN05Hohk5lA0Mjuvcvy40FUwg=; b=hqUOBO8a7yQVls/kS564ywNWnJr35/oX56veN6sAkoZqDnxFii/RuIA+QAWx/LIsOQ 2Skc+RskEeRqAOP2bXv5qeUe96h4kNboQ108gOUBmbioCuuZ262YN316mAn+RPgKJmFp i3TSNVtCUECsvV7TD49aZrDjB1JFN565w4G/ko3nPFnfyYqUQhdNNkMTynWPQcR5rzOL pmvT5+UNAD80zzHtEb4zhOFssqffRYmcbywxmbSByACH/OEkfOpl/n4ySpz+/0n3+P6+ yFGUj0/qou8sRAHIcuRcfiISyXlZC4vmkP9wNzo8NDTSrDTLivb58AMKQvf9pszEZ1XU 8pdA== 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=edQyUB3GEvh33HPxtgbN05Hohk5lA0Mjuvcvy40FUwg=; b=oDvQ5BJBZxaHQ+u3vwZO9m7qGdzKVkLGv8k0XpSVzrDXMk5TUX6TquANEk6lU/tMsg GdYnmo7zcgnW2c87fSOMV5fSIrDtGk5H7i96Pwg05JK1RuBtlWecU39VbpgL4mqNmggJ m7n4nv3yxC/m91AsQ8GEwegvOqoor8sPsTuDjK6ZnTxB84+aGqDQYB1TslMjTEAHKmzL meYXvG71XiaG8S6Rr+GTezC/BBMGDcO3k2hzBumjD8LV2biAZtxNUOh7F53kIFGCHBfL 3iA3hdK55sp1V/O3iUPYM9YOotqFphbt11NHI5x8L8hVAsGcYEJ95G12AKTiDnnQHErI gHpg== X-Gm-Message-State: AJIora+EKHfkhaGmSD4y4N72Z1WTQeH98+/tjpCJea8UOUWftP0YH4OR M2QVT88jkxn39hrL1aeZQu83z7lQokJQ99o4Yjk= X-Google-Smtp-Source: AGRyM1u2YXmLXq3FL17A+fYelfHmWAfm2g3AOB8E4Q9cXdEiOq/yanHjutFIASYPiafn6ZXmOoY9D8KPx4YmAE/ApnY= X-Received: by 2002:a25:3383:0:b0:66b:6205:1583 with SMTP id z125-20020a253383000000b0066b62051583mr9439804ybz.387.1656598737202; Thu, 30 Jun 2022 07:18:57 -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 16:18:44 +0200 Message-ID: To: Dan Ackroyd Cc: Rowan Tommins , 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 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: > > 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. > > 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. > > > 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". > > cheers > Dan > Ack > > > For this code, comment the var_dump in/out to affect the lifetime of the object. > > class ResourceType > { > public function __destruct() { > echo "Resource is released.\n"; > } > } > > function get_callback() > { > $some_resource = new ResourceType(); > $fn = fn() { > // // why is my lock released? > var_dump($some_resource); > // "Free that resource" > $some_resource = null; > }; > return $fn; > } > > $fn = get_callback(); > echo "Before callback\n"; > $fn(); > echo "After callback\n"; > > // Without var_dump > Resource is released. > Before callback > After callback > > // With var_dump > Before callback > object(ResourceType)#1 (0) { > } > After callback > Resource is released. > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php > 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. 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)? 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'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.