Newsgroups: php.internals
Path: news.php.net
Xref: news.php.net php.internals:119728
Return-Path: <nicolas.grekas@gmail.com>
Delivered-To: mailing list internals@lists.php.net
Received: (qmail 35545 invoked from network); 16 Mar 2023 17:59:49 -0000
Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5)
  by pb1.pair.com with SMTP; 16 Mar 2023 17:59:49 -0000
Received: from php-smtp4.php.net (localhost [127.0.0.1])
	by php-smtp4.php.net (Postfix) with ESMTP id 6020B1804AA
	for <internals@lists.php.net>; Thu, 16 Mar 2023 10:59:48 -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: <nicolas.grekas@gmail.com>
Received: from mail-wr1-f50.google.com (mail-wr1-f50.google.com [209.85.221.50])
	(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 <internals@lists.php.net>; Thu, 16 Mar 2023 10:59:47 -0700 (PDT)
Received: by mail-wr1-f50.google.com with SMTP id q16so2332707wrw.2
        for <internals@lists.php.net>; Thu, 16 Mar 2023 10:59:47 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20210112; t=1678989586;
        h=cc:to:subject:message-id:date:from:in-reply-to:references
         :mime-version:from:to:cc:subject:date:message-id:reply-to;
        bh=4p0tj1Epo5Ypxpn9PAYv0e8D/DaS3bskiQun9G7kEfY=;
        b=hJWZ5/rqinBWXc3jpUXJnwPspPDHggSpZ6p5G/ZEeJkVIAvCmNum88uIJyX+BILidH
         eWHFoLzGDOLicd3y0qs1Q+EyGl1fs911PUU+LT6ifU0JEbDPOYv/EjCmBjrcHGrV7Q3j
         56WMqnthjm5d0SM0QnlboQC/f64S+BLaS08P25UkMyEsk24xmTHaAVgzWJ3//gBlc4kc
         lqnuyyjdlSk6mCp+8WEvlGZnLTctjYFdxczOvACompJ1J6UGWkv/aeKp3xJSyyGbKVVx
         2L5gyS1NbjjGOE0IbHx0fqDC2W5zyMeVf6VGfvViQCCWwgB0ofF65dTOhr4ExSo4nxG6
         bPTg==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20210112; t=1678989586;
        h=cc: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=4p0tj1Epo5Ypxpn9PAYv0e8D/DaS3bskiQun9G7kEfY=;
        b=JIH7zmiIpIuunDGIcxfMakHOWmRcKc4KTVjieKHKbpdplJsU2xZq7W24auVt3Ls1jo
         zgKsailFHEiydiEKLCIdP8URdBG+CO8IgKCZeAsC/7tKdEpwUJto03RqT2Dlp2DiIW+O
         F7syQVfX/HNpwBQsrJJ2i/1BteRA+7U5hR1yL/Nj1vC9y0KGh9p7JOhzUGrbZjcQAaD3
         xbMEtva1kjuOPJVmHY4Pq74fzO2pIFLxxARXUG6ACaSGemL5uxHGchsdNpcYoiDIx6Ef
         UsZlUOxvdh2C0sIQFWdH3Wo3b4u/21wieuDwnx152F6z1cFMTVYVUm+JjYB3fqYY+Xe0
         F7Xw==
X-Gm-Message-State: AO0yUKVe78QfqJR0lsOOg6lQUSGEUZO5NP2N2yqThMOPWJzscKFBnYfb
	ivbC0kZINf0GKuc3CkgsWq17jdzA+R1EwOVei5Q=
X-Google-Smtp-Source: AK7set//tFL82cuml3mN274/f2dfWyYDVL05T+VabkU+di4QEitbpsBB4uL2MHI7DI1V3i+B+MQOLmmqaAicSssw1Yw=
X-Received: by 2002:a05:6000:615:b0:2cf:ef9f:33f6 with SMTP id
 bn21-20020a056000061500b002cfef9f33f6mr99579wrb.1.1678989586505; Thu, 16 Mar
 2023 10:59:46 -0700 (PDT)
MIME-Version: 1.0
References: <8a9a04b3-45e6-47dd-dee8-0f524e49bd37@gmail.com>
 <CAOWwgpnPxDg0VWCtwn9ALAdiHNn1SFjLAsvVJiMaR-QQc0fU2A@mail.gmail.com> <CALKiJKrCXgWmBzWB_HfcqbrA8G4xLDzdryOkcy+JjUCWm4fC5Q@mail.gmail.com>
In-Reply-To: <CALKiJKrCXgWmBzWB_HfcqbrA8G4xLDzdryOkcy+JjUCWm4fC5Q@mail.gmail.com>
Date: Thu, 16 Mar 2023 18:59:35 +0100
Message-ID: <CAOWwgpmOFek0oZt9aOg44nCbcbcY7J3T-Ghw=UvFFd6Ugr=pRA@mail.gmail.com>
To: Rowan Tommins <rowan.collins@gmail.com>
Cc: PHP Internals <internals@lists.php.net>
Content-Type: multipart/alternative; boundary="000000000000e2a49105f7083985"
Subject: Re: [PHP-DEV] Brainstorming idea: inline syntax for lexical
 (captured) variables
From: nicolas.grekas+php@gmail.com (Nicolas Grekas)

--000000000000e2a49105f7083985
Content-Type: text/plain; charset="UTF-8"

> > 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.
>

We could define the "use" as declaring + setting the properties before the
constructor is called, if any.
But I'm also fine making both constructs conflict: when there is a
constructor, the boilerplate saved by the "use" becomes really low.
No strong opinion either. There could be other factors to consider.



> > 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) {}
>

I like that!

--000000000000e2a49105f7083985--