Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:120026 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 99516 invoked from network); 13 Apr 2023 12:40:07 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 13 Apr 2023 12:40:07 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id EA26718037E for ; Thu, 13 Apr 2023 05:40:06 -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,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-f46.google.com (mail-wr1-f46.google.com [209.85.221.46]) (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, 13 Apr 2023 05:40:06 -0700 (PDT) Received: by mail-wr1-f46.google.com with SMTP id e16so823157wra.6 for ; Thu, 13 Apr 2023 05:40:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1681389605; x=1683981605; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :from:to:cc:subject:date:message-id:reply-to; bh=f2K3y9VJ2I15ZTQUvCWvuQ5jueVDKtMQIQ7F/AOaeY4=; b=qPSA/4IRZSwisGwu67a3VSf/fznJ6ClMLeq6fygGoA4TFmZNLYjCK3Jf5zRl1y+xOS 5h9HlmGpW620n9SIXe+1yvj3G+7RfopUYDv83EU1QK3H9oVdtuvv2ngFlQDZApVezal4 oljxei+UAPhN8z3juFK9ouDYpyk+3HxhxUyxx7GBhBQc9zx653ZiK4gBhwsqdMgBEXfC l+bXUh2tkOVyJUn7xHcvGs9Pyi7DZu1vHjOnXzgjulpcZQfAaJfBLo2yIPFfK+lBDqor xRLIfR4b5kEoTYYK/8zjfyqQyQDPRVO3ySGIYz7ASMbgh5IiKW/wz45R7k6DBfAljw0/ kcNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681389605; x=1683981605; 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=f2K3y9VJ2I15ZTQUvCWvuQ5jueVDKtMQIQ7F/AOaeY4=; b=KULeqANAw3ggBBJGs3/J+cXHF4p8mybxnI5lKJRfvvlxB0johXPKflmKSrF4cKKqHb 3WPspASc/L3ZTJuDKXLhZyFSO1jyfVbN/jH5Q+hS5kdGu5BNUbFCgfViMlsvIRUwOKQ0 XoaupMekcbjZhZdq3sot92QpV5g1lw833GNDFuIexJGI0G+xkTBeuYJdradoQSQBKiHW lJul4ba/V86f4s+PXJZo8v3AC0dYIiEyZUGjuFiO/DsP7TL9i5PcrSE0EvJm2gFDeYSK o+JcOdOhd1zrJUaaBiT4GL4zx/cXIcgFAIZ+1yAZBKgK60I9qQJSec13o6xhEIg6aEmn 6ldg== X-Gm-Message-State: AAQBX9fKNh01HeSTshWFnIHBFbnYJAlhT1VMJ/yH9uGlwi7LWHTwkxXU 0TltewM/YjqCTJQfq679UC7AS4BHuMnUHfwFsGlv7apnXCsGBA== X-Google-Smtp-Source: AKy350bqs1NTOy+Jw8sw4c9SLaljgUUi63xaZWj66bMFvWG+4KnB7rH7i6r+pZpzH84iSdK/kJIbERoHgdXKgbNHzAQ= X-Received: by 2002:adf:f5ce:0:b0:2f6:21c:72a3 with SMTP id k14-20020adff5ce000000b002f6021c72a3mr278880wrp.5.1681389604691; Thu, 13 Apr 2023 05:40:04 -0700 (PDT) MIME-Version: 1.0 References: <8a9a04b3-45e6-47dd-dee8-0f524e49bd37@gmail.com> <52a5a80d-f03f-5e96-4188-272a705e3fdd@gmail.com> In-Reply-To: Date: Thu, 13 Apr 2023 14:39:53 +0200 Message-ID: To: php internals Content-Type: multipart/alternative; boundary="0000000000001de3a305f93706d2" Subject: Re: [PHP-DEV] Brainstorming idea: inline syntax for lexical (captured) variables From: nicolas.grekas+php@gmail.com (Nicolas Grekas) --0000000000001de3a305f93706d2 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hi Rowan, hi all! Le ven. 17 mars 2023 =C3=A0 15:51, Larry Garfield = a =C3=A9crit : > On Thu, Mar 16, 2023, at 6:05 PM, Rowan Tommins wrote: > > On 16/03/2023 22:14, Larry Garfield wrote: > >> Wouldn't the functionality described boil down to essentially just > materializing into a few extra lines in the constructor? At least to my > ignorant non-engine brain it seems straightforward to have this: > >> > >> $a =3D 1; > >> $b =3D 2; > >> $c =3D 3; > >> > >> $o =3D new class ($a, $b) use ($c) { > >> public function __construct(private int $a, private int $b) {} > >> public function something() {} > >> } > >> > >> Desugar to this: > >> > >> $c =3D class ($a, $b) use ($c) { > >> private $c; > >> public function __construct(private int $a, private int $b) { > >> $this->c =3D 3; // By value only, so this should be fine? > >> } > >> public function something() {} > >> } > > > > > > Not quite - as Ilija pointed out, the class definition gets compiled > > once, but the capture needs to happen for every instance, with > > (potentially) different values of $c. In other words, $c needs to be > > injected as a constructor argument, not a constant in the class > definition. > > > > That's still fine, in principle - you can compile to this: > > > > $o =3D class ($a, $b, $c) { > > public function __construct(private int $a, private int $b, private > $c) { > > } > > public function something() {} > > } > > > > Or once constructor promotion is de-sugared as well, this: > > > > $o =3D class ($a, $b, $c) { > > private int $a; > > private int $b; > > private $c; > > > > public function __construct($a, $b, $c) { > > $this->a =3D $a; // from constructor promotion > > $this->b =3D $b; // from constructor promotion > > $this->c =3D $c; // from use() statement > > // other lines from body of constructor go here > > } > > public function something() {} > > } > > > > > > It just introduces a lot of extra cases to handle: > > > > * If there's no constructor, create one > > * If there is a constructor with other arguments, merge the argument > > lists; since there will then be an explicit argument list to "new > > class()", merge those lists as well > > * Maybe different handling if those other arguments are already using > > constructor promotion, as in this example > > * If there are existing lines in the constructor body, combine those > > with the auto-generated assignments > > > > > > Which is why I'm thinking a first implementation would be reasonable > > which just took this approach: > > > > * "new class" can either have an argument list or a use() statement, no= t > > both > > * the use() statement generates a constructor at the top of the class > > * if the class body already contains a constructor, the compiler will > > complain that you have two methods named "__construct" > > Ah, fair enough. I'd want to see a better error message than that (which > would be confusing for people who don't know what they're looking for), b= ut > otherwise that's a reasonable first iteration. Especially as I can't > recall when I last had an anon class constructor that was doing anything > other than manual closures. :-) > I created this draft RFC to help move things forward: https://wiki.php.net/rfc/syntax-to-capture-variables-when-declaring-anonymo= us-classes Please let me know your early thoughts and I'd be happy to move it to "under discussion". I'd also need someone for the implementation as I doubt I'll be able to write it myself in a reasonable time! Cheers, Nicolas --0000000000001de3a305f93706d2--