Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:118136 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 91621 invoked from network); 30 Jun 2022 13:45:24 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 30 Jun 2022 13:45:24 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 4CF501804D0 for ; Thu, 30 Jun 2022 08:36:56 -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-f44.google.com (mail-wm1-f44.google.com [209.85.128.44]) (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 08:36:55 -0700 (PDT) Received: by mail-wm1-f44.google.com with SMTP id be14-20020a05600c1e8e00b003a04a458c54so1913130wmb.3 for ; Thu, 30 Jun 2022 08:36:55 -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 :references:from:to:in-reply-to:content-transfer-encoding; bh=Zdlvc5UoL/Au9fNXPysjGGzwoKDA1kGGcgxh8g69k6I=; b=EYffQVDzsJtIfTw6yPaDNreOdAjHGyRmQ7g6jpTQcfGnGrn/++AvtD3sLuv7zfA7vQ nqpMREyjU+UsaX99ms5VwKc8fuNy7PHYZqYg1i5zKxFaraK3qGwf+0UaMdF+twkGE8bt 3ALsB6JkC3jvXtXqeJU/olRdb/k+1AXHugUXG5Z1A3Z0B9VdBHXk9Z7o5i3DAfDJKh8Q RIo68oG1LgcuE3RRFtkYBsU25qHXcwbMbhj4H5hLydXQJJrgE6uugWaGh43b0Gu3wR1R wKHYNqoOt2P5E2YWwJRnr+nJGgegx4ylBs5dMKhNXmLLWmcId2wb90/bsEf2mPNVQQRA lF3A== 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:references:from:to:in-reply-to :content-transfer-encoding; bh=Zdlvc5UoL/Au9fNXPysjGGzwoKDA1kGGcgxh8g69k6I=; b=2gvo6005Lk4gnlzNh/db2UX60EZdgxzdR1YJuK8u4Yj+fu6DEo3pmYrDo8nazR0vZY 8QF6CbA5u6cMTxNVu5XKFRdKOyBte26Cnu2uWiHgR3UandMIi97200lwwnQ7k3TrG6q+ bT1MdtRRhONP2dmn6DhKjHim8TICh/uW+LqZn6a8Hx19mVl5NtFSJhd2YEsb77y90weF ZyM22HknkNHADJ7Iy2NX4KLOWpic6BSct0B8B+f+bGq8O7YKM5kwQid73iaT9uIs4gQc l/whI/0+Xf/GudW8iZXrUFl+VKWYvuzaVwgpuCXtWP1fINdc6FDnqB7q6Rq4u3pWt5Nj qkuQ== X-Gm-Message-State: AJIora+fiQgk4nw77mKAHj87LiCggBvmxkd74PJYB/MONuSEbAbHfOwN EnUziOOoIyzsF8zfcSCqTNlIf6FUCGo= X-Google-Smtp-Source: AGRyM1tHHj2Etr+0oxUN9jT4quumCAmKXGGkS5Ijx4ip7YeCGTBD+Wdq1hCdaNySHJ02DD3N4kt5qA== X-Received: by 2002:a05:600c:1908:b0:3a0:998c:313d with SMTP id j8-20020a05600c190800b003a0998c313dmr11705624wmq.19.1656603414533; Thu, 30 Jun 2022 08:36:54 -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 u20-20020a05600c19d400b0039c4f53c4fdsm3758286wmq.45.2022.06.30.08.36.52 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 30 Jun 2022 08:36:53 -0700 (PDT) Message-ID: <3ecd49be-7e02-03f0-ba27-efd5a1e63617@gmail.com> Date: Thu, 30 Jun 2022 16:36:52 +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 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> To: PHP Internals In-Reply-To: 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 30/06/2022 14:25, Dan Ackroyd wrote: > 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. Please look again at the detailed explanation I gave, and the examples that Guilliam posted. Your example can *only* work if the variable is captured by reference, because it requires the statement *inside* the closure to have an effect on a variable *outside* the closure. No version of auto-capture has ever proposed capturing by reference. If instead of $some_resource = null; you wrote $some_container->some_resource = null; then that would have an effect on the object, but the "optimisation" would be irrelevant because the use of $some_container itself is not an assignment. > 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". Right now, assigning (or unsetting) a variable is the *only* way to force it to be local. That's why I said I would be more likely to support this feature alongside a "var" or "let" keyword to make such variables explicit. Not being able to have local variables *at all* other than by very careful variable naming is a terrible idea. Just to re-iterate, here's your new example with explicit capture, to demonstrate that the closure *does not and cannot free the resource*: https://3v4l.org/WrTb5 class ResourceType { public function __destruct() { echo "Resource is released.\n"; } } function get_callback() { $some_resource = new ResourceType(); $fn = function() use ($some_resource) { // this line does nothing // it overwrites a local variable which is never read // next time the closure runs, it will start again as the captured value $some_resource = null; }; return $fn; } $fn = get_callback(); echo "Before callback\n"; $fn(); echo "After callback\n"; unset($some_resource); echo "After destroying outer var\n"; // the captured reference is still live here, no matter how many times we call $fn() // only destroying the closure frees it unset($fn); echo "After destroying closure\n"; One way of thinking of it is that assignments inside a closure are assignments to a local variable, which "shadow" any captured variable with the same name. If all you do with a variable is shadow it, then it is illogical to consider it "used" in that function. On 30/06/2022 15:18, Robert Landers wrote: > 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)? I would expect so, yes. It could be considered a bug that the arrow function implementation currently "over-captures" variables, and it only wasn't a higher priority in Nikita's RFC because it is extremely rare that a single expression closure would have any local variables. Indeed, that lack of local scope is one of the big reasons why I and others supported that RFC, because it avoids all the confusion evident in today's messages. Regards, -- Rowan Tommins [IMSoP]