Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:118126 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 51723 invoked from network); 30 Jun 2022 06:27:49 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 30 Jun 2022 06:27:49 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 8064A180212 for ; Thu, 30 Jun 2022 01:19:15 -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-f48.google.com (mail-wr1-f48.google.com [209.85.221.48]) (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 01:19:14 -0700 (PDT) Received: by mail-wr1-f48.google.com with SMTP id k22so26068082wrd.6 for ; Thu, 30 Jun 2022 01:19:14 -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=7cEHzoy9GPuPklMMVxy485rGhKKSl4ProqdHfr0gKf0=; b=UweWiHxGQi3tewp+nfXE8jrIEQA45X+38M81Z5Mub6O5FeCXWsrEscfrkL/0VIwcCX j9oQrPOa+qcsojxDZU9bHt1Sjjuh9GmSqOsPkiqLUDDzk0m5Rh3PRk88ieY20bK08jfP XQGPU6yzsv8X+6DWxMOJltc7HGHSW4X5/hd//19D7yvF2pmNx+NTZCkrN4cD3hulMd7i 7TGEW/PVvxqiOeYL+plbTrnb4yfrUcMzAo8AvxVLmaneT/6qs4qF8do9gl4bauWz5x0L BOLp0pg52HLlgLZo/GETgtUPZaeY11okMeX7KxO1wrVBLqQzkh73M/npqRpkrv2TVvVs w7XQ== 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=7cEHzoy9GPuPklMMVxy485rGhKKSl4ProqdHfr0gKf0=; b=EKV53roszaMiJeddrGT70RFM9a3uDU4GAk3kvIO1OQ/X8mI/5FxtcorOvZMhQjbi/w rUFIBTdgxrrUKcAILFBQiFhoXTVb1rMG14WBQwvm5RkeC4FWVP6GI9Y0uIzIlukggWTp b/YaIUnEHic+omVi46bZkUHMQy2PjhAjggNq0wPvOco8zeqLDhmMygMnF8if1qlJA6Lc D5joeF2WTwaMhMaZTVveasg11wAG+T4KWyUZM/KmRjg138MADnzNIMZlhB1UhGLKlK8C UOP0flHse1/6CxdrFCqv2/9ckAOhd2XkdVrlS/NpZEQ5qZeaGWiMFxfP71YKqAuY+xnf Gx4w== X-Gm-Message-State: AJIora8zmjdK1aCcdgNqivJrZUf7OCP2z1NiLLgzvJ1+cseEn3UYT/aP ThFI24x9eXSrL0sx57AXXi0BUgu9ezI= X-Google-Smtp-Source: AGRyM1vDNpqtq2GvefqyCOYVUqB9n9b0xAcL6QL05Nt7u6rcKaM+owAV6QWEjlYeihZrRmIdJirpIA== X-Received: by 2002:adf:fc0c:0:b0:21b:8422:b55 with SMTP id i12-20020adffc0c000000b0021b84220b55mr7575274wrr.321.1656577153543; Thu, 30 Jun 2022 01:19:13 -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 bn24-20020a056000061800b0020fe35aec4bsm18046066wrb.70.2022.06.30.01.19.11 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 30 Jun 2022 01:19:11 -0700 (PDT) Message-ID: <50894bde-74cc-b5a4-4bb7-ba0e682f952c@gmail.com> Date: Thu, 30 Jun 2022 09:19:05 +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@lists.php.net References: <2b35605f-8da8-46b1-aec3-00bd1bfe47fd@www.fastmail.com> <84eb5551-cfaa-42e6-9a74-ae229d5e269c@www.fastmail.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 29/06/2022 23:31, Dan Ackroyd wrote: > Imagine some code that looks like this: > > // Acquire some resource e.g. an exclusive lock. > $some_resource = acquire_some_resource(); > > $fn = fn () { > // Free that resource > $some_resource = null; > } > > // do some stuff that assumes the exclusive > // lock is still active. > > // call the callback that we 'know' frees the resource > $fn(); > > That's a not unreasonable piece of code to write For that to work, it would require the variable to be captured by reference, not value. Writing to a variable captured by value, like writing to a parameter passed by value, is just writing to a local variable. In fact, the "optimisation" is in my opinion a critical part of the semantics, to avoid the opposite problem: // Acquire some resource e.g. an exclusive lock. $some_resource = acquire_some_resource(); $fn = fn () {     // Use a variable that happens to have the same name     // A naive implementation would see $some_resource mentioned, and capture it     // Over-writing the local variable here makes no difference; the closure still holds the value for next time     $some_resource = 'hello'; } // Free what we believe is the last pointer, to trigger the destructor unset($some_resource); // If $some_resource gets captured, it can only be released by destroying the closure unset($fn); Regards, -- Rowan Tommins [IMSoP]