Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:119732 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 57718 invoked from network); 16 Mar 2023 23:05:38 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 16 Mar 2023 23:05:38 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id D16EC1804D7 for ; Thu, 16 Mar 2023 16:05:37 -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,NICE_REPLY_A, 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-f42.google.com (mail-wm1-f42.google.com [209.85.128.42]) (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 16:05:37 -0700 (PDT) Received: by mail-wm1-f42.google.com with SMTP id az3-20020a05600c600300b003ed2920d585so4124592wmb.2 for ; Thu, 16 Mar 2023 16:05:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1679007936; h=content-transfer-encoding:in-reply-to:from:references:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=hbvXF748CfHgqvGBA/JhfCE5HgbXjGHx9kAZkXctreE=; b=JgD+Vu/w7yrpfHR3DY6nFULq1IAY0weo8ejUEMiFwtn7EF0swm0dbCy8Eulunwxtxu LFczGgtpXrt7OM8Jkc8vjVSdspkZTw+MRZc5d744I+/7rJYrg6H5miwbv67R2NhYtq8K tqWd/kS5ooWiQINnlwgU4x/l9AmhVoscsNPBI51tmkqTLu8RGtryuf9i4o72JSy9dCsh RtN2lNgSlnGRHumSpI4ONIn7X486mboaFdjDSPF5FJlgRC/0neChxaOgLESOv3Xa7cgn 3pXnoWSiVmq41zMyuOVIMLdUco6/se+u3CxzNHhZ4wcia2kuW4CIFHEtnjEUqukCpJ5M 4fug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679007936; h=content-transfer-encoding:in-reply-to:from:references:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=hbvXF748CfHgqvGBA/JhfCE5HgbXjGHx9kAZkXctreE=; b=zE4s5TcnPy5xLGrWKZ2fejW+M0Oswwf42qWFPcyI7sIO20lt0/YfhLI78FUK1ZgWge MZNlAQNUJtsAClZWgbahyF4IBJL2aMUaY13wxcYcJPIGHR27MRJSoiUZRDWDDQszRzDF UAU6m2xHzMuxQLe/rvdmZoKWMpDqmWaU6NuJPbvlSSs88drZFHUmLicVKW/3GkP/b1ef EqeKsSZwHyJVPGcaf4xEeeUNL40e9eiz2xQKyzbSknbrSRjZVkQ/+pgXfRi5CgIv2DI1 vIbELqqM9HEgezhKjMV6ox3cB0uBuAKmmBizCNuBKYfpsv9ogxjmNZpR7k09pewvAdAz Juiw== X-Gm-Message-State: AO0yUKXaPQ+CfEkwehMXbu92PIto5YYyYDSEhFfcEHA+o1wBtIiOsQIU 53DlgOaGJbevdHME/4pPXv2/WgLIPF0= X-Google-Smtp-Source: AK7set/IwoZosX+jq3vFIJM8mMcNobwWcxD2oH5EuOdW2fX50AZKIcw4kXzl32cCd3l9x9PLj5Nenw== X-Received: by 2002:a05:600c:4f89:b0:3dc:5b88:e6dd with SMTP id n9-20020a05600c4f8900b003dc5b88e6ddmr24521830wmq.10.1679007935798; Thu, 16 Mar 2023 16:05:35 -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 d22-20020a05600c34d600b003e20970175dsm6231249wmq.32.2023.03.16.16.05.35 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 16 Mar 2023 16:05:35 -0700 (PDT) Message-ID: <52a5a80d-f03f-5e96-4188-272a705e3fdd@gmail.com> Date: Thu, 16 Mar 2023 23:05:35 +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: internals@lists.php.net References: <8a9a04b3-45e6-47dd-dee8-0f524e49bd37@gmail.com> In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Brainstorming idea: inline syntax for lexical (captured) variables From: rowan.collins@gmail.com (Rowan Tommins) 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" Regards, -- Rowan Tommins [IMSoP]