Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:119720 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 78082 invoked from network); 16 Mar 2023 10:33:47 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 16 Mar 2023 10:33:47 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 350F3180381 for ; Thu, 16 Mar 2023 03:33:46 -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.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,FREEMAIL_REPLY, HTML_MESSAGE,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-f41.google.com (mail-wr1-f41.google.com [209.85.221.41]) (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, 16 Mar 2023 03:33:45 -0700 (PDT) Received: by mail-wr1-f41.google.com with SMTP id v16so1057661wrn.0 for ; Thu, 16 Mar 2023 03:33:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1678962824; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :from:to:cc:subject:date:message-id:reply-to; bh=ul2GuEfY6BBHwGjmW6B6QaB/nuOWOZ4XDYCyoRcz8FI=; b=E2yDLBgiLnJWSshN/TO2Rkr1urh64IGo4rbBfrxV5vTHP1KMfOHltO2AiCvGB+nVRg QjMOKu/ouWYRwLCkUqrGhQeJlIiAG+8dQnYIeRJUko5VKAXPNg+MO3fJMe3RPct/jyhu iEbIHCGpxu9oFw7mtKjUlXwzYT/LYgNwTBaBprscH1OClgyE/YAWUsvLimj3u4LJa9xa ZPDMh0H8RYJ7EEuCZOasS1cZEHN2bdNHmZFpOhqqMIY6pTqwuRItEbwWsx5wyr/F4v57 Yuonlxj0IMHuIMZp1IDfOp4q3u+j2nbcX3nM0ic4NC3EhXTAB36QAsYLskODnRlYom6X Zpdw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678962824; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ul2GuEfY6BBHwGjmW6B6QaB/nuOWOZ4XDYCyoRcz8FI=; b=Nd/BLFHZgHR/vlNJz3FGmvn5Xw36h/bvww4EQ2+bS9BxyBKIL7PRnXBoUccsFmH5tz xsczD/gN9DobX2ii19HhTOCETBr/xiUm5J174ypxn7u+qpepp1b6xgL2MX4zMMYdKgTW a5PNMfJszJnSzeswkXfXYnU5oYprkIgDUV0t51nKUs5pJfuRhuI8yTlv7rVJcCJb31f1 q8mjBsOyGGBak9h7P+jVhplVeR5NUBmAoDffnKQ5cxklLno8aHottxiu1sbypOWQLUZa MFletq2cG/ivSgWULlf9OWtHDN1zRRvYFypY5QskYXWDb7Gqim9/pkmrbVz6sXryiPMf +7Rw== X-Gm-Message-State: AO0yUKVyZiW9LwjQWxKhhJhProGwZbb6gyG6AqpIp79MIf2ezZ9vXZYD ng2NhVN4AQTTZfSbS0kvU17WgyPFr0l74NJ77X82w6OEtfs= X-Google-Smtp-Source: AK7set/VABgJvjnw0134b9e4JKGEOzgi+wxTLCo4CyfvcbWikVZu2YiySbhAQcTONlkvFRxhVWcpxz14rgC3IMLnu80= X-Received: by 2002:adf:f48a:0:b0:2ce:ab76:6ab5 with SMTP id l10-20020adff48a000000b002ceab766ab5mr1030825wro.6.1678962824205; Thu, 16 Mar 2023 03:33:44 -0700 (PDT) MIME-Version: 1.0 References: <8a9a04b3-45e6-47dd-dee8-0f524e49bd37@gmail.com> In-Reply-To: Date: Thu, 16 Mar 2023 10:33:32 +0000 Message-ID: To: PHP Internals Content-Type: multipart/alternative; boundary="000000000000ba647d05f701fe82" Subject: Re: [PHP-DEV] Brainstorming idea: inline syntax for lexical (captured) variables From: rowan.collins@gmail.com (Rowan Tommins) --000000000000ba647d05f701fe82 Content-Type: text/plain; charset="UTF-8" On Thu, 16 Mar 2023 at 09:28, Nicolas Grekas wrote: > > To overcome the issues spotted in the thread, what about doing some sort > of CPP instead of autocapture? > > new class (...$arguments) use ($outer) extends Foo { > public function getIt() { > return $this->outer; > } > } > > This would be the equivalent of this: > > new class ($outer, ...$arguments) extends Foo { > public function __construct(public mixed $outer, ...$arguments) { > parent::__construct(...$arguments); > } > public function getIt() { > return $this->outer; > } > } > I was actually just thinking about exactly that approach, and wondering it would be possible to do it entirely as an AST rewrite. My only uncertainty so far is what to do with an actual constructor in the class, like this: new class($custom) use ($captured) { public function __construct($custom) { // Duplicate definition error? // Silently renamed and called from the generated constructor? // Merged into the body after the generated lines? } } Forbidding it wouldn't be the worst restriction, but if there was some per-instance setup logic needed, not being able to write a constructor body might be a pain. > And we could also allow this for better type expressivity: > new class (...$arguments) use (private int $outer) extends Foo { > // ... > } > I was going to suggest an "as" clause, similar to traits, which would also allow naming the property differently from the source variable: $foo = 42; $name = 'Bob'; $class = new class use ($foo as private int $counter, $name as readonly string) {} Equivalent to: $foo = 42; $name = 'Bob'; $class = new class($foo, $name) { public function __construct(private int $counter, public readonly string $name) {} } Or in full: $foo = 42; $name = 'Bob'; $class = new class($foo, $name) { private int $counter; public readonly string $name; public function __construct(int $counter, string $name) { $this->counter = $counter; $this->name = $name; } } Regards, -- Rowan Tommins [IMSoP] --000000000000ba647d05f701fe82--