Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:117932 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 27147 invoked from network); 13 Jun 2022 11:26:25 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 13 Jun 2022 11:26:25 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id F26D51804C6 for ; Mon, 13 Jun 2022 06:13:39 -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-f48.google.com (mail-wm1-f48.google.com [209.85.128.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 ; Mon, 13 Jun 2022 06:13:39 -0700 (PDT) Received: by mail-wm1-f48.google.com with SMTP id j5-20020a05600c1c0500b0039c5dbbfa48so4543886wms.5 for ; Mon, 13 Jun 2022 06:13:39 -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=NREoQOMgRPHXexLauz6dnvOYry2fhyQ6JbW/QH6UaVY=; b=VolHbPpyQ9rgG6kYayPUPvYmwslwIUzbc6PxsuJAjcQdwiGDVOoNXIPkrtSPgHQ4SJ gdiku5GkrumOe8oJQKeb7nZSHBql7ip+uXAds/8/7CboQ771FbJK8MDdoYgCK2FLLYHU /tWO+n59VuJH7ZaOF/H0Gqp/0O7WId34nHOZsWjnetlkbFSDt+T6luUK4oMYCRDFvzHy dUQwaJ3BrJL/Q/MCU8Sj2M8Sb+OfEYUbFqp99F4WO1EenIQjf+rm3eEpv45w8nsYyu7a Hq1X7LfteCOWAaJAbv9kJ9kO3ZJWWrVvvPELHcsWPr7aftFslpv/5l/0WXPWY9aku6R8 SP8g== 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=NREoQOMgRPHXexLauz6dnvOYry2fhyQ6JbW/QH6UaVY=; b=pYUpHJdSbaA3QChLQ/JMydNx1n8ZqtehHNo1F2mVt2st7NND+9jYHLOGO+IptnP0LL MJOkds80Kx6xWlLpqGi6QTtxUG3C7OxjmuoHTAj5zbeqoekofXNlmd8Ofigv1yXoFPgU y0M6x3MVTNNE+3DL9qEtMc4aypF+ma1Z9h0+YHHkYSSsGX1bZ2BN/yQ/He2Xs//aL+te Y4yRpDKS99wk6KriZUMnKhXAb2409wVDCdD1W+yKG7hCx9UAeguYuAZUa17yPNlGDa+l Z4pmUBgVYidjT51+UWhSzS0Shf4lPavF2gF4pNYSaV6hAW8ClnjrK3T+OJXijwAm++69 BycA== X-Gm-Message-State: AOAM530gwDEj0xondBd9KQECgD9Qq9OG+4XXnzola0x9ymtt0CyRKy91 wE3gvyn/5+IfecPBUFvtVtElfJOWs/I= X-Google-Smtp-Source: ABdhPJx6zesGnUU+6HIOSnF968ECXBpwlj9IcOQotDMXEX7tMcSfV46gWnGWS4ruNOBET0HXpyU+Hg== X-Received: by 2002:a05:600c:2105:b0:39c:381c:1e13 with SMTP id u5-20020a05600c210500b0039c381c1e13mr14969087wml.189.1655126018353; Mon, 13 Jun 2022 06:13:38 -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 r124-20020a1c2b82000000b0039c97cc82fbsm2296944wmr.15.2022.06.13.06.13.35 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 13 Jun 2022 06:13:36 -0700 (PDT) Message-ID: <48f4f560-c051-5b14-9434-d28dcc34184e@gmail.com> Date: Mon, 13 Jun 2022 14:13:34 +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> <3564811.oiGErgHkdL@arnaud-t490> In-Reply-To: <3564811.oiGErgHkdL@arnaud-t490> 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 13/06/2022 13:29, Arnaud Le Blanc wrote: > Following your comment, I have clarified a few things in the "Auto-capture > semantics" section. This includes a list of way in which these effects can be > observed. These are really marginal cases that are not relevant for most > programs. I'm not sure I agree that all of these are marginal, or with the way you've characterised them... > Note that destructor timing is undefined in PHP, especially when reference cycles exist. Outside of reference cycles, which are pretty rare and generally easy to avoid, PHP's destructors are entirely deterministic. Unlike in fully garbage-collected languages, you can use a plain object to implement an "RAII" pattern - e.g. the constructor locks a file and the destructor unlocks it; or the constructor starts a transaction, and the destructor rolls it back if not yet committed. A related case is resource lifetime: file and network handles are guaranteed to be closed when they go out of scope, and accidentally taking an extra copy of their "value" can prevent that. > It ends up capturing the same variables that would have been captured by a manually curated |use| list. This slightly muddles two different questions: 1) Given a well-written closure, where all variables are either clearly local or clearly intended to be captured, does the implementation do a good job of distinguishing them? 2) Given a badly-written closure, where variables are accidentally ambiguous, what side-effects might the user experience? The answer to question 1 seems to be yes, the implementation does a good job, and that's good news, and thank you for working on it. That is not the same, however, as saying that question 2 is never relevant. Consider the following, adapted from an example in the RFC: $filter = fn ($user) {     if ( $user->id !== -1 ) {         $guest = $repository->findByUserId($user->id);     }     return isset($guest) && in_array($guest->id, $guestsIds); }; This is not particularly great code, but it works ... unless the parent scope happens to have a variable named $guest, which will then be bound to the closure, since there is a path where it is read before being written. In this case, side effects include: * The behaviour will change based on the captured value of $guest * Any resources held by that value will be held until $filter is destructed, rather than when $guest is destructed Whether the risk of these side effects is a big problem is up for debate, but it's wrong to suggest they don't exist. Regards, -- Rowan Tommins [IMSoP]