Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:118128 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 62215 invoked from network); 30 Jun 2022 08:25:54 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 30 Jun 2022 08:25:54 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 5ABBA1804D0 for ; Thu, 30 Jun 2022 03:17:23 -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-wr1-f49.google.com (mail-wr1-f49.google.com [209.85.221.49]) (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 03:17:22 -0700 (PDT) Received: by mail-wr1-f49.google.com with SMTP id b26so14157896wrc.2 for ; Thu, 30 Jun 2022 03:17:22 -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:to :references:from:in-reply-to:content-transfer-encoding; bh=dypaz8haxIoX650W5pVhn1n7QDDFJCk2TV3PYWg1nQQ=; b=JzYPoufu/HcwKjWtuqj0xtoeivn+nlyGEXMAqoPOqPTk5NWbMAXem5X7kZ/C0ZyZD9 +VY7VLcsuD8zw99xF7SZm18So9esIV2vPb+qHGwkC53eDPcDS1I2fRPuTuc3ik4CYjom D0YycCS0VXvE0YYEOgcZ7RTkXpFk/WYQFYrlLHzvp84pEpwXivaKtFD0SFXBLWrO3zTH txVgb8s7wIj9nCpxlkqa9cdCRiRE2khpaCOLaYc6IH1yo3LkkouMswvqtsaPOIFH3T5p HiFhGc1NOvhQ0RGQkTiw3bM93YuFF+f5YVuutlIyVyYD9uzK0eN1b361pUHYQ6Ay8ykM RkTA== 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:to:references:from:in-reply-to :content-transfer-encoding; bh=dypaz8haxIoX650W5pVhn1n7QDDFJCk2TV3PYWg1nQQ=; b=Lq/FqcyDTgf3vEAL6yp0BhbFJfsGyiz1/YTRA8CoEdDNgy/EBfq/1mJWDnLpz4FzII VQkxbsnSVSmC8KVuIfjYRAcVDT/SqNwCw75DNyEFf8Hoam0rtqd+9bTUv7mv8Z2y+4hF g4gnDwWLM48NiRnOZGdSbanzb4pqrMAi2eWIiom7ygUbO0ZsEoh5DQ02mxqaerifpZD1 pQ3RUBOp/KvR2dpSHjTuJsdJvfGuHYcRgMtFNaMVjNP2+PnjfLOEB67/eoLAc5TocyJQ yJpevYIp+06CICxXqtJJvUj6mhShxn/gAtMOqPeii1gGS3NJHryPW16UO0hcugQEnTSe S5TQ== X-Gm-Message-State: AJIora/jsiS9ZV2yEgT15BNGCznG9tHu/ozeR1TEST+6qzBY56WMNmly sSX5qVGkaFolHHJ4hBxwULPESArsk8g= X-Google-Smtp-Source: AGRyM1vONeUFv1Jw3Ce3KlvZJXsL/ENAZajxVptOHuuCalFyf9WyUDBGnhItRAirRKpQ6ZR/N2Vhlw== X-Received: by 2002:adf:dbcd:0:b0:21b:868a:4cbe with SMTP id e13-20020adfdbcd000000b0021b868a4cbemr7397763wrj.522.1656584241539; Thu, 30 Jun 2022 03:17:21 -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 g34-20020a05600c4ca200b0039c7dbafa7asm1954913wmp.19.2022.06.30.03.17.20 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 30 Jun 2022 03:17:20 -0700 (PDT) Message-ID: <2dcd5fe0-7284-138b-990c-512966141951@gmail.com> Date: Thu, 30 Jun 2022 11:17:19 +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 To: internals References: <2b35605f-8da8-46b1-aec3-00bd1bfe47fd@www.fastmail.com> <84eb5551-cfaa-42e6-9a74-ae229d5e269c@www.fastmail.com> <50894bde-74cc-b5a4-4bb7-ba0e682f952c@gmail.com> In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Subject: Re: [PHP-DEV] [RFC] Short Closures 2, aka auto-capture take 3 From: rowan.collins@gmail.com (Rowan Tommins) On 30/06/2022 10:20, Robert Landers wrote: > I think their suggested code would work (at least currently in PHP) by > the simple fact they would increase the reference count on that > object/resource until they set it as null. However, with the > "optimization," the reference count will never be incremented and thus > fail to work as defined. No, the captured value is tied to the lifetime of the closure itself, not the variable inside the closure. $some_resource = acquire_some_resource(); // refcount=1 (outer $some_resource) $fn = function() use ($some_resource) {     $some_resource = null; } // refcount=2 (outer $some_resource, closure $fn) $fn(); // during execution, refcount is 3 (outer $some_resource, closure $fn, local $some_resource) // once the local variable is written to, the refcount goes back to 2 (outer $some_resource, closure $fn) unset($some_resource); // refcount=1 (closure $fn) $fn(); // the captured variable always starts with its original value, regardless of how many times you execute the function // during execution, refcount is now 2 (closure $fn, local $some_resource) // after execution, refcount is still 1 (closure $fn) unset($fn); // only now does the refcount go down to 0 and trigger the destructor The only way for it to work would be using capture by reference (not supported by the proposed short syntax): $some_resource = acquire_some_resource(); // refcount=1: simple variable $fn = function() use (&$some_resource) {     $some_resource = null; } // refcount=1: a reference set with 2 members (outer $some_resource, closure $fn) $fn(); // during execution, we have a reference set with 3 members (outer $some_resource, closure $fn, local $some_resource) // the assignment assigns to this reference set, changing the value referenced by all 3 members // refcount on the resource drops from 1 to 0, triggering the destructor $fn(); // because it was captured by reference, the initial value of $some_resource in the closure has now changed Regards, -- Rowan Tommins [IMSoP]