Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:118127 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 57382 invoked from network); 30 Jun 2022 07:29:10 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 30 Jun 2022 07:29:10 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 5F058180212 for ; Thu, 30 Jun 2022 02:20:38 -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-yw1-f180.google.com (mail-yw1-f180.google.com [209.85.128.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 02:20:38 -0700 (PDT) Received: by mail-yw1-f180.google.com with SMTP id 00721157ae682-318889e6a2cso172715227b3.1 for ; Thu, 30 Jun 2022 02:20:38 -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=5J9njMRHEI2bd2P9FNiY8Va3lx/N8z3wCpcbDJrtuP8=; b=lEZppcuU0JRrKvSOPJRNZ3KsQo1pNCEEmK13TTmHkgrIQqoQBL85+5fWOxTLKunkEg JHBWkJEVQGZbiiWgVwqU/sFu9la2aSd7qcKOMYEuq5TXWfcQGtOLnqdLtsgVr9jtXDGV 5QhD738T8HoawJQYJjQWRp1mZenZe36W06nC86KYGQxe5nBwjMIiqU3ch+64UhjXjXEy HzSWZQH++fSuNZGUiNE9prHqUers9q63TAfnr6x1cbWHnD5RJjgyXUn+a39UEyNqzu1B P2bpX7A91IeIJsa1sWzdlXq1ag4GOVM3dOaqKZm+kEMaBGXFpP6SoYHMVLUIncqoHFrC Hscw== 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=5J9njMRHEI2bd2P9FNiY8Va3lx/N8z3wCpcbDJrtuP8=; b=YEMUz4/ofo9GgeDbdM919y+VMOkb+nLR3svDFVt1WjzsjiCRpstHgoAx3PHo6GSLHi RuXNEo38gmNVnC257ax0btOZfyoDqXO/2/7UnD+WR5lkRMjTVvYCgOopyl/Rns4rXIso W18SEqCJCfGDOUXqOWcIzc0pRwdOZzujq5440Vd6w+Fin3vLtgU5pvmYNA5LcHdtc2JG K2gsa8xEQ4LluEeEi5v5JsR+ptaFvFXCjcdvuYFwkFT07FhIDAQZgCKGV5ond7KIN5ko CldDpWseCL0IpXpFu1bJtMOer7s8B6KXDN0MOUOEBRtPF0ZqkVCiSQBis5rXsU51VURL Fq3w== X-Gm-Message-State: AJIora9L/VEy7Yg/ApY5XpPs5wrsm0zVK4JvG4VEgjuoZMtnqokISVXi iQvH/ONnONW9MpDVYIF2mRP+ekmzc54qRuHLDsI= X-Google-Smtp-Source: AGRyM1uzY06S7IjS7tPVKzpZqLSN+qLNEeQ6ETVmLc9w1KzWjFdtGmjERggLlUkmxH8Xpx+qbGqBkjBcTfe2kvSeFUE= X-Received: by 2002:a81:5e87:0:b0:31b:d751:dc41 with SMTP id s129-20020a815e87000000b0031bd751dc41mr9110394ywb.230.1656580837365; Thu, 30 Jun 2022 02:20:37 -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: <50894bde-74cc-b5a4-4bb7-ba0e682f952c@gmail.com> Date: Thu, 30 Jun 2022 11:20:24 +0200 Message-ID: To: Rowan Tommins Cc: 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 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.