Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:118129 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 64041 invoked from network); 30 Jun 2022 08:37:33 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 30 Jun 2022 08:37:33 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id B39EE180089 for ; Thu, 30 Jun 2022 03:29:02 -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 03:29:02 -0700 (PDT) Received: by mail-yb1-f175.google.com with SMTP id h187so30740505ybg.0 for ; Thu, 30 Jun 2022 03:29:02 -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=MK2rtW1ouSIBI/m1dh3Zff78sBJ7LhGuHf5OTIVvSt0=; b=gLRpyKWVBrDFt2/mG3wkRYVmTjW5Ga1XbxYQPQZwzGtStx47YV50LB5nGW1NU4+cgR 77YiEFQ9HNqIYWnpcWb0hdDnvN+430F6/iZBxpfY9ptN5svlz7PLBx6K1s1g4Hoa7L8b 7bqhwXDbcjqFzVTcdeDVjMVt8FyBcO+Hkc0sh5wQH968CMJeZug/g39EAEqbz2diuIfp aH7WnjXOYQev9k/XAocOkqstdRg5k7n7fVf5NXBOj6sE7CaOkO3vyqr8vrHpyWnVC9dT xPxUgtU85pSor4DPYevkXUoY9gsVrWKD+oneAMNzwcwByjBWN/u5S/li9TkyAHajJ/Yz osxQ== 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=MK2rtW1ouSIBI/m1dh3Zff78sBJ7LhGuHf5OTIVvSt0=; b=sCwfa/xYhj46OcANYd4Cl8igBnt77exMUpCe8mI5ugJvhB1j9Wm3QSrOxzttXJiPDL Ms3lYKDWJltgbrcnndB13sSS0ydZJifKno/tUrDxynNrYQEtQS09KVcoxVcOQXjF7mYy fr7jkQjLLMpgFuE/8vfUXmDH903Faaxj4QqRSMX4bgOmeEoI/B1MjhPPHvamwV8/MDgj HIXJ8YJwlWQivX7MokvOGaWZ9b8x1T+3l2RnIpzJ2CR/6fpPxr1HjXcVE74xbIt2V4qL AJvZ6ECv/a3S3k38CBDlOx8UTFGFIWBATXQADn5nhdAMcNqYRduQCPRHX3xKMtBzQQq1 5Nbg== X-Gm-Message-State: AJIora/bOU1Co3NHk5bnowR6AIbiJO/cRE1MyJ1QEPHE3wxyuEULp6zm S6+RDRhZ5JlUnVRKyew+yz81h5aebvGrFJycqA== X-Google-Smtp-Source: AGRyM1ulWfT6zgd97GWnakwOuK2nBVoJwb/VIcdEMh0q5D11Qfhgi3ZyWRn2XjNOBduv31Ep69GClxbwZpd6AAWTgvA= X-Received: by 2002:a05:6902:689:b0:66d:ab7b:119b with SMTP id i9-20020a056902068900b0066dab7b119bmr2302919ybt.402.1656584941795; Thu, 30 Jun 2022 03:29:01 -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> In-Reply-To: Date: Thu, 30 Jun 2022 12:28:50 +0200 Message-ID: To: Robert Landers 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: guilliam.xavier@gmail.com (Guilliam Xavier) On Thu, Jun 30, 2022 at 11:20 AM Robert Landers wrote: > > On Thu, Jun 30, 2022 at 10:19 AM Rowan Tommins wrote: > > > > 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] > > > > -- > > PHP Internals - PHP Runtime Development Mailing List > > To unsubscribe, visit: https://www.php.net/unsub.php > > > > > For that to work, it would require the variable to be captured by > > reference, not value. > > 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. > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php > No offense, but why don't you just try it? Please see equivalents of: - Dan's code: https://3v4l.org/51jXY => doesn't "work" - Dan's code with capture by reference (as said by Rowan): https://3v4l.org/JoUVi => "works" - Rowan's code: https://3v4l.org/7ZVv3 => shows the "problem" with capture PS: I see that Rowan just replied with refcount explanations. I agree (but am sending this anyway) Regards, -- Guilliam Xavier