Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:117937 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 47758 invoked from network); 13 Jun 2022 15:52:19 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 13 Jun 2022 15:52:19 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id C27CC1804D4 for ; Mon, 13 Jun 2022 10:39:37 -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=-0.6 required=5.0 tests=BAYES_00,BODY_8BITS, 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-f42.google.com (mail-wm1-f42.google.com [209.85.128.42]) (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 10:39:37 -0700 (PDT) Received: by mail-wm1-f42.google.com with SMTP id z9so3363898wmf.3 for ; Mon, 13 Jun 2022 10:39:37 -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=BksbpuqB3wOhc/bx6lbyQGDKanh3BmfsOmzEwrnE+OY=; b=SETnRnM+jtxZA3BsfFYlXP0zUZ6Hlr5uGZPouDI7a89LD1IVJiVty2+yXY3x5oBquw 69dZ0B8ptfOIkz5RRxSJS9YyTa1edmtaG9fqvTfZSZ4aYKEU8hWNMAE+sV7AnsS7pkrR PiPZVJFpD3iAzy6rjbyNZAmF9gRRagdk603VRRJU71K8WF4zA+x51FI62aPtu08f7hK0 VZgOKLoTrgjzq29FBjHsOTWZeiqXn5NSBPFr4KmLqeIfzL0if0TWLGs+d5lnvLA3zj2v rat0Y3jLDTQ/vkvw7jGN1Ucyp2mwk8K6/yRHtd6Lcv9d/5VF2P3iGgMDP5uahyyBoq4/ kK6g== 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=BksbpuqB3wOhc/bx6lbyQGDKanh3BmfsOmzEwrnE+OY=; b=vNE/SIxuaHqCRKblMea4gy6zUZA8nM+QZQalLZhmNPdwsLWQD54yUoEKfWbgzFlkjZ 93WQPhxQjKwDWJ9GGHO+gmneE327xOJbuS8rsJ0XZ45ammdF2RAGD9RwWuks76hqCjR0 vhtkGZOK95H8YtxqHedN20Rr0q5PHapTA+2C3yEQfcXv3fsKK+Dq63NLR4Iw4zS3QUD6 IAaVBvrD4sxkWuzDPn0Kq7wd0FDizh1usJWh4uzJvP1u2eYGTPV9CZdS6vrehUqYNrxQ 0huF/UtT9U5W8mrgO8us6qkmTaUjfJ84rUltl31yoSffi15BxIVhVeIhoe2YMD8qd/UP f9kw== X-Gm-Message-State: AOAM530UDhoRETKXoaP6J9gipYuk6UUFnVuNv3dg/kTHoK2FIGU59b64 IxCo8opUc+2VmdEk+JmwssjcRWLScnY= X-Google-Smtp-Source: ABdhPJwxdGXTn8QABmD940GWiR1moE9LHnmtGWpddrTr75TLIzgeO6YACa22nE1O2K7BsPaKYWZnxg== X-Received: by 2002:a1c:f416:0:b0:39c:4778:74cd with SMTP id z22-20020a1cf416000000b0039c477874cdmr16117141wma.128.1655141976227; Mon, 13 Jun 2022 10:39:36 -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 t63-20020a1c4642000000b003974cb37a94sm14401981wma.22.2022.06.13.10.39.35 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 13 Jun 2022 10:39:35 -0700 (PDT) Message-ID: <42cdb2d9-485d-cf5c-4cce-b9c1f8c2084e@gmail.com> Date: Mon, 13 Jun 2022 18:39: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> <8310f3fd-0011-970e-5379-b2b6e03942b2@gmail.com> <2347345.PIDvDuAF1L@arnaud-t490> <8422e11c-4a3f-9f99-56ce-884491dfe08e@gmail.com> <39470114-2c35-4c71-b8ef-c8d3ce3c3555@www.fastmail.com> In-Reply-To: <39470114-2c35-4c71-b8ef-c8d3ce3c3555@www.fastmail.com> 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 14:52, Larry Garfield wrote: >> That's one perspective. The other perspective is that the proposal is to >> extend closure syntax to support automatic capture. > As noted before, this is a distinction without a difference. It's a difference in focus which is very evident in some of the comments on this thread. For instance, Arnaud assumed that adding "use(*)" would require a change to arrow functions, whereas that never even occurred to me, because we're looking at the feature through a different lens. >> By the way, the current RFC implies you could write this: >> >> fn() use (&$myRef, $a) { $myRef = $a * $b; } > The RFC already covers that. $b will be auto-captured by value from scope if it exists. See the "Explicit capture" section and its example. So it does. I find that extremely confusing; I think it would be clearer to error for that case, changing the proposal to: > Short Closures support explicit by-reference capture with the |use| keyword. Combining a short closure with explicit by-value capture produces an error. And the example to: $a = 1; fn () use (&$b) {     return $a + $b; // $a is auto-captured by value                          // $b is explicitly captured by reference } Clearer syntax for this has been cited previously as an advantage of use(*) or use(...): $a = 1; function () use (&$b, ...) { // read as "use $b by reference, everything else by value"     return $a + $b; } > 1. Auto-capture could still over-capture without people realizing it. Whether this is actually an issue in practice (or would be) is hard to say with certainty; I'm not sure if it's possible to make an educated guess based on a top-1000 analysis, so we're all trying to predict the future. I tried to make very explicit what I was and was not disputing: > 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. The RFC seems to be implying that the implementation removes the side effects, but it does not, it is users paying attention to their code which will remove the side effects. > Arguably it's less of an issue only because short-closures are, well, short, so less likely to reuse variables unintentionally. Our current short closures aren't just a single *statement*, they're a single *expression*, and that's a really significant difference, because it means to all intents and purposes *they have no local scope*. (You can create and use a local variable within one expression, but it requires the kind of twisted code that only happens in code golf.) If there are no local variables, there is nothing to be accidentally captured. That's why the current implementation doesn't bother optimising which variables it captures - it's pretty safe to assume that *all* variables in the expression are either parameters or captured. > 2. The syntactic indicator that "auto capture will happen". The RFC says "fn". You're recommending "use(*)". However, changing the indicator syntax would do nothing to improve point 1. The reason I think it would be better is because it is a more *intentional* syntax: the author of the code is more likely to think "I'm using an auto-capture closure, rather than an explicit-capture closure, what effect will that have?" and readers of the code are more likely to think "hm, this is using auto-capture, I wonder which variables are local, and which are captured?" Of course they can still guess wrong, but I don't think "fn" vs "function" is a strong enough clue. > (Making fn and function synonyms sounds like it would have a lot more knock-on effects that feel very out of scope at present.) Off the top of my head, I can't think of any, but I admit I haven't tried hacking it into the parser to see if anything explodes. Regards, -- Rowan Tommins [IMSoP]