Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:119696 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 31523 invoked from network); 14 Mar 2023 20:41:11 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 14 Mar 2023 20:41:11 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 8162A1804AA for ; Tue, 14 Mar 2023 13:41:07 -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=1.3 required=5.0 tests=BAYES_20,BODY_8BITS, 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-wm1-f53.google.com (mail-wm1-f53.google.com [209.85.128.53]) (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 ; Tue, 14 Mar 2023 13:41:07 -0700 (PDT) Received: by mail-wm1-f53.google.com with SMTP id k25-20020a7bc419000000b003ed23114fa7so5341331wmi.4 for ; Tue, 14 Mar 2023 13:41:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1678826466; h=content-transfer-encoding:to:subject:from:content-language :user-agent:mime-version:date:message-id:from:to:cc:subject:date :message-id:reply-to; bh=1uyhJvEogtmI4TPMlmLgA20OLn6QNezmwymeEjeQZn0=; b=pg5Q0GwBmbrtMuToFrthkk+H45Ui/M3zdr/EIDIIW9n7Q0bkcfujgpi0iW1rIjFegB LfsSOjbts4wd+9eobTZgWNLfbvsO3NYKNDKOJYn9RCvpEq8aAMsgdMOaWrlCm1IZTfHq CyMjPV2RhJhWQJkwk4u9eJmY7QEkg4nS3lx1YvzCuJ6FuqhyoAXVdlJoPO2L6FBoP1hq wpZJW1uvrNjszgCYHEQKJ9j2mq15P1pTssi1Y8+bB7C7sf/787010yaJU7gLLMBJ/9XK FREE7CfxflOkmCFe0BVo/M4YQvw7A2Y3V4+oRTK5V8DxTuMOtQ3O6hKrKYNkQIsvk0wM pNvg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678826466; h=content-transfer-encoding:to:subject:from:content-language :user-agent:mime-version:date:message-id:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=1uyhJvEogtmI4TPMlmLgA20OLn6QNezmwymeEjeQZn0=; b=NWIlN8kfTIwQUQMkPZVbW5IdPeKNHAemdeZbxl9tkc2W3JPHC77tVeXb1lAxmg9XF/ Yy7oZmdmnE0y5SpBzPj14onxOXfQX4bFUHTLsCcmbxWo8/uTFpzhnXElVSZv1LvCyDNR Lgav3TsMAbtXgcZ/BMmSL3406HHWd00aWpIZU4cPj/GXhNxSt/neYceq+c5cAnSdugdh nC1rxw9eKG9+UsMuUZvoTYASEqdfWZ5fk308VYgVOtiV/F0bZ8QP1/UqihnYrjjxHR12 N5Tx3qMoOduwj35J8kXdbtoT/dlqCrODEQWVNPYRjHLh4QZU80SBaHcZ8gUzymOy/25K Pxsg== X-Gm-Message-State: AO0yUKVvB2aZNrwpt7jOySdp3asfBqQIUnJ1Zu0BsG2kVwimzz0r4bUN lgMmx6r9WrhcUAEBoM8ZXpUYz/k6ScI= X-Google-Smtp-Source: AK7set8sNNtNrDbHhWA5xbETOcYsDhfMq3uH1UjrBEE5gE+FNF6ZCZrqzPj772eC24z3zV3d4NWPFA== X-Received: by 2002:a05:600c:3509:b0:3ed:29f3:d69f with SMTP id h9-20020a05600c350900b003ed29f3d69fmr5304135wmq.36.1678826465723; Tue, 14 Mar 2023 13:41:05 -0700 (PDT) Received: from [192.168.0.22] (cpc83311-brig21-2-0-cust191.3-3.cable.virginm.net. [86.20.40.192]) by smtp.googlemail.com with ESMTPSA id x17-20020a1c7c11000000b003eae73f0fc1sm3771801wmc.18.2023.03.14.13.41.04 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 14 Mar 2023 13:41:05 -0700 (PDT) Message-ID: <8a9a04b3-45e6-47dd-dee8-0f524e49bd37@gmail.com> Date: Tue, 14 Mar 2023 20:41:04 +0000 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.8.0 Content-Language: en-GB To: PHP Internals Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Subject: Brainstorming idea: inline syntax for lexical (captured) variables From: rowan.collins@gmail.com (Rowan Tommins) Hi all, I have been pondering for a while how to improve the anonymous class syntax to allow "capturing" of values from the outer scope, and came up with the idea of a special variable marker for "lexically captured variable" - instead of $foo, you would write $!foo or $^foo (I quite like the "upwardness" of $^). To give a simple example, values can only pass into the anonymous class via its constructor, like this: function foo(int $outer) {     return new class($outer) {         public function __construct(             private int $myProp         ) {}         public function getIt() {             return $this->myProp;         }     }; } The idea is that you would instead be able to reference an outer variable directly anywhere in the declaration, removing a lot of boilerplate: function foo(int $outer) {     return new class {         public function getIt() {             return $^outer;         }     }; } The outer variable would be captured by value, and carried around with the instance, like existing closures. I suggest it is also treated as readonly, and visible strictly in the lexical definition, not generally in private scope (it couldn't be referenced from an applied trait, for instance). Using it to initialise a property or local variable would allow you to give it an explicit scope, while still avoiding the constructor: function foo(int $outer) {     private $inner = $!outer;     return new class {         public function getIt() {             $this->inner++;             return $this->inner;         }     }; } It then occurred to me that the same syntax could be used in multi-statement anonymous functions instead of an explicit use() statement. This strikes a different balance between conciseness and explicitness than full automatic capture: $before = function($x) use ($y, $z) {     $a = $x * $y;     return do_something($a, $z); } $after = function($x) {     $a = $x * $^y;     return do_something($a, $^z); } To keep this message short, I've put some more examples and thoughts into a GitHub Gist here: https://gist.github.com/IMSoP/4157af05c79b3df4c4853f5a58766341 I'd be interested to hear anyone's thoughts - is this a promising idea to explore, or have I gone completely off the rails? Regards, -- Rowan Tommins [IMSoP]