Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:119741 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 18502 invoked from network); 17 Mar 2023 14:51:45 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 17 Mar 2023 14:51:45 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 143ED180543 for ; Fri, 17 Mar 2023 07:51:45 -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.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_NONE, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS19151 66.111.4.0/24 X-Spam-Virus: No X-Envelope-From: Received: from out4-smtp.messagingengine.com (out4-smtp.messagingengine.com [66.111.4.28]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Fri, 17 Mar 2023 07:51:45 -0700 (PDT) Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id B2C6A5C00C8 for ; Fri, 17 Mar 2023 10:51:44 -0400 (EDT) Received: from imap50 ([10.202.2.100]) by compute4.internal (MEProxy); Fri, 17 Mar 2023 10:51:44 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= garfieldtech.com; h=cc:content-type:content-type:date:date:from :from:in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to; s=fm2; t=1679064704; x= 1679151104; bh=MRoTsqbOF0lIknWp0ARUkTMmSKWGP5HdbHOyAfvEfno=; b=G 8P2KHl21CVzyzAMaC4paoQnwLVIrJsiABDoQuMwZV1qhxHLLvkAKL+g9Egq81LSg 9ZXcM6+2Ji5Xzpo6rhhBvcYoAgVPEoXG9nC2r7QLzIYiwDhkLdk9yX7Wra3gru7n AfeJehP6cyhfkkXH8W2gHIbnrh1gwjDiHhHHDD2C6xijjIAhcWKmKjvgHglMSUWr o2ASDMODrA81tkf3Hx1J1wYE8lApZqShxtNbVz7JZWSYJO9I9g3GT5C4nfQXdSwR jz8480uaOv9QZRoI4bD6Tqvp6gp9lr/jdHIhmmvPkQ9XNhL+781vbViP3JBxt+Gh rRPxEg0GHOjeO5UqSnmnQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:sender:subject :subject:to:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm2; t=1679064704; x=1679151104; bh=MRoTsqbOF0lIk nWp0ARUkTMmSKWGP5HdbHOyAfvEfno=; b=rG1Qp+DqR5iNSY21IeowK0OfXuJSa S3islroCEZnsfXuiQjGdBLfaxs3nGrQ6T0d/a0WOX+mysl2hT+ArFeCqaQZuCLuY Kd+4qQZbLK9EjFJ6IdFUvzeOGQqx4rQGPgO8by84DEE4qvz4f50OuhLBhSLbCLT+ Jwyom4PaftzqaAMczUDtK3rwPW7wzOcIM4F0tq1mPzPZLYxwWlzTSM5nZWY0ukgB IpBarB0XUyYuJEXJ8x1VMcKpr2CXPplUCwNoYzPesyXW4sLiecxhMwhkPe8ETXTC XDNfFGM226L8gjvoC9uDYT0dXFC1FuBlkIADeH7+T+VJXOaTrSn/1DK+Q== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrvdefvddgieejucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepofgfggfkjghffffhvffutgesthdtredtreertdenucfhrhhomhepfdfnrghr rhihucfirghrfhhivghlugdfuceolhgrrhhrhiesghgrrhhfihgvlhguthgvtghhrdgtoh hmqeenucggtffrrghtthgvrhhnpeeglefgkeduiedvvdetffeujefftdfhjeeiveehgfff keduveektddvledvvdfffeenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmh grihhlfhhrohhmpehlrghrrhihsehgrghrfhhivghlughtvggthhdrtghomh X-ME-Proxy: Feedback-ID: i8414410d:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id 7D5311700093; Fri, 17 Mar 2023 10:51:44 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.9.0-alpha0-221-gec32977366-fm-20230306.001-gec329773 Mime-Version: 1.0 Message-ID: In-Reply-To: <52a5a80d-f03f-5e96-4188-272a705e3fdd@gmail.com> References: <8a9a04b3-45e6-47dd-dee8-0f524e49bd37@gmail.com> <52a5a80d-f03f-5e96-4188-272a705e3fdd@gmail.com> Date: Fri, 17 Mar 2023 09:51:24 -0500 To: "php internals" Content-Type: text/plain Subject: Re: [PHP-DEV] Brainstorming idea: inline syntax for lexical (captured) variables From: larry@garfieldtech.com ("Larry Garfield") 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 = 1; >> $b = 2; >> $c = 3; >> >> $o = new class ($a, $b) use ($c) { >> public function __construct(private int $a, private int $b) {} >> public function something() {} >> } >> >> Desugar to this: >> >> $c = class ($a, $b) use ($c) { >> private $c; >> public function __construct(private int $a, private int $b) { >> $this->c = 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 = 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 = class ($a, $b, $c) { > private int $a; > private int $b; > private $c; > > public function __construct($a, $b, $c) { > $this->a = $a; // from constructor promotion > $this->b = $b; // from constructor promotion > $this->c = $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, not > 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), but 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. :-) --Larry Garfield