Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:117933 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 29940 invoked from network); 13 Jun 2022 11:49:15 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 13 Jun 2022 11:49:15 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id B188C1804D4 for ; Mon, 13 Jun 2022 06:36:30 -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-f43.google.com (mail-wr1-f43.google.com [209.85.221.43]) (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:36:30 -0700 (PDT) Received: by mail-wr1-f43.google.com with SMTP id k16so7174458wrg.7 for ; Mon, 13 Jun 2022 06:36:30 -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=+FgSKfFSocjk1a2GBuUeBKS6p0t1pwpOubEtB2nrsR4=; b=ENUq4cFjfW1WvDxAjU2/ExpIWXVYvTtqEQa48yBd5TjnXwfZw8k/z9giQBPcobHERV wlzs8Sra9dswu1eIiVBGzOphz5C/m8YpZojLaWkbbUQ4V10EMMog2Eqnn3x0UG2KpVL2 5sSL1L2sNOCWxbcRwCU+1qIPLLi/4RM2ZGw6VA2i4kK+u9gMlt71bVhM8DE1Om9M4O16 wp9kY6+x+0i7zs4IYToJ+7xy2Kelc3LsGQo/Jj0ViNmrUKMNREoDYFkV8yBszquakIkv DJ78Z6YdnprtWSi/zioNPBUps/T6EBmBi3gK+IDzXokEzHC5mVKGZFpiqPk+PxQO0M4X lTlw== 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=+FgSKfFSocjk1a2GBuUeBKS6p0t1pwpOubEtB2nrsR4=; b=kgrAXLw2zHkQ4ZMcosVrRfOjO3EiMHnFoVlW+fByhu4h2ehhvRgUx39Ii4naHbPowF GqbvzeEUdwMY22qzPwPiT/aSh28vQlcH4W89lS6eu2biCtYeUjqjvtU3J6Cm2bkj5//x amDwd9i9vYoIbVii3jW07xkOnOVDPpxXGWt8MHOzfaYwjk4ryX9rWL10muaenf4slwoH DlgaMlRt4YPV234p8QWESI/S+TKvyGbmEd3xFzkryMx8k57izYXReRzWZBDazRRD+igh OPPR6ouShN1aKIG3YvLf3+uymSqnAXRDxG+ncvDa+nejBJrCIJK6b1r9fs6hg7PMVnXc hWpg== X-Gm-Message-State: AOAM531V5IS41QpQae/6FFuvzZDmn38TQjPmKizMznzd3Z02cpND9jSz 8ntj3cG9RKFdSX8F8HjLwKm3zJdnU/s= X-Google-Smtp-Source: ABdhPJw2FkayBfos9Ruuap1L5j4SVmO3JJ4aOCHjSNOv/meDFd9uIBdZ36MM8PlKa6yjX6jllyTeXA== X-Received: by 2002:adf:c614:0:b0:215:cd85:bd88 with SMTP id n20-20020adfc614000000b00215cd85bd88mr47403396wrg.437.1655127388882; Mon, 13 Jun 2022 06:36:28 -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 u7-20020a05600c19c700b003973b9d0447sm17220262wmq.36.2022.06.13.06.36.27 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 13 Jun 2022 06:36:28 -0700 (PDT) Message-ID: <8422e11c-4a3f-9f99-56ce-884491dfe08e@gmail.com> Date: Mon, 13 Jun 2022 14:36:26 +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> <8310f3fd-0011-970e-5379-b2b6e03942b2@gmail.com> <2347345.PIDvDuAF1L@arnaud-t490> In-Reply-To: <2347345.PIDvDuAF1L@arnaud-t490> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit 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:57, Arnaud Le Blanc wrote: > The proposal is to extend the Arrow Functions syntax so that it allows > multiple statements. That's one perspective. The other perspective is that the proposal is to extend closure syntax to support automatic capture. > Currently the `use()` syntax co-exists with auto-capture, but we could change > it so that an explicit `use()` list disables auto-capture instead: > > ```php > fn () use ($a) { } // Would capture $a and disable auto-capture > fn () use () { } // Would capture nothing and disable auto-capture > ``` That's an interesting idea. I was coming from the other direction, but it might make sense I guess. By the way, the current RFC implies you could write this: fn() use (&$myRef, $a) { $myRef = $a * $b; } It's clear that $myRef is captured by reference, and $a by value; but what about $b? Is it local to the closure as it would be in a "long" closure, or implicitly captured by value as it would be with no "use" statement? It might be best for such mixtures to raise an error. > Unfortunately, Arrow Functions already auto-capture today, so requiring a > `use(*)` to enable auto-capture would be a breaking change. I'm not suggesting any change to arrow functions, just the ability to write "use(*)" (or "use(...)") in all the place you can write "use($foo)" today. I don't think that introduces any problems, if you think of "fn" as an alternative spelling of "function", and "=>" as expanding to "use(*) { return" >> I don't find the comparison to a foreach loop very convincing. Loops are >> still only accessing variables while the function is running, not saving >> them to be used at some indeterminate later time. > Do you have an example where this would be a problem? I didn't say anything was a problem; I just said that the comparison didn't make sense, because the scenarios are so different. > Auto-capture in PHP is by-value. This makes this impossible. It also makes > explicit declarations non-necessary and much less useful. > Live-variable analysis is mentioned in as part of implementation details. It > should not be necessary to understand these details to understand the behavior > of auto-capture. As noted in my other e-mail, by-value capture can still have side effects, so users may still want to ensure that their code is free of such side effects. Currently, the only way to do so is to understand the "implementation details" of which variables will be captured, and perhaps add dummy statements like "$foo = null;" or "unset($foo);" to make sure of it. Regards, -- Rowan Tommins [IMSoP]