Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:119699 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 41809 invoked from network); 14 Mar 2023 22:44:14 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 14 Mar 2023 22:44:14 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id D4C9E1804F8 for ; Tue, 14 Mar 2023 15:44:13 -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=-0.6 required=5.0 tests=BAYES_00,BODY_8BITS, 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-f51.google.com (mail-wm1-f51.google.com [209.85.128.51]) (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 ; Tue, 14 Mar 2023 15:44:13 -0700 (PDT) Received: by mail-wm1-f51.google.com with SMTP id p16so11391750wmq.5 for ; Tue, 14 Mar 2023 15:44:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1678833852; 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=sbghPpVXqxnNFYxCt2AqxmQkYyfFEn3pfbZn0emsaAA=; b=IQUio6jKYVf1tttR+8HFB7gyY8WxqtddmSYp2jVHc2h1CWtQyjudvxfSInx92hrGm6 zQgdjv4H3jZjxJh9/5m/ooisz9+rUDFPkHgbnpRyAv/+Z1iFCf7LZSi1Snu36wmEGfOv KVkJM5T6txJoAZSwsFfD2K6b1VifGfHuu/E2Blf3XobIObqdsnaFzJHLu1UqidMxbF5j JFepYL5r644VJJM/1ddorfIY5fVrLookvfn2GE+nc4YW3qSjPANJpx2oXa8NJ7qElTzI ksONY6WDFrMQAPCH9f4jYw8wAc/zlbLKKPBQaABQdwrKIia0qmeEmlcNPk3A6cKeDy/z fZgA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678833852; 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=sbghPpVXqxnNFYxCt2AqxmQkYyfFEn3pfbZn0emsaAA=; b=Z6nOvDLriXhKNGBY98rVZE93HDJk8eYGkRQ62H15jO/aVhRGTMBTQKgdYxvyTMGSqh RBKWdYJu4w7Rr4sryGMNCYqpo68VhOghbLuw0UACqFAzroywKqo63Xrj9iXUjDbzuXp3 U1qpsoGZuLFBlpTQRo6CW/MJQW1opevbLyVOm8VPRl3grDnSkn5BYZ4PTdVSckU/U25/ 3XWkLk6UYXsOEY0xjNLmh9DFPRlRwpfzcXpR6qjM/MZPwNhZJn9MMFUFM6BrELE0aCPl MWvtovR67Yd0o4uGAMyC92rWNyeaoMVIbUaRQUFpKepgb9KkDCDHGGxMnvVcYBMFcHL2 kvDQ== X-Gm-Message-State: AO0yUKVFx7FOvluVZx1vJvnKV+myJYh3fL21evTDFFwlr5hdSu43Wp8o NZebkIMyAPxu/X6sBRdcyiBn1UcU2AI= X-Google-Smtp-Source: AK7set8qG4b+Kd1j5RMuu/Iq5YnrYp5Ed3B0msIDrCoZUFTPX+WUAtIP2YsHNq0bB0xYAlIcDOQqWA== X-Received: by 2002:a05:600c:190c:b0:3eb:38e6:f65d with SMTP id j12-20020a05600c190c00b003eb38e6f65dmr15766330wmq.11.1678833852405; Tue, 14 Mar 2023 15:44:12 -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 u7-20020a5d4687000000b002c5544b3a69sm3053083wrq.89.2023.03.14.15.44.11 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 14 Mar 2023 15:44:11 -0700 (PDT) Message-ID: Date: Tue, 14 Mar 2023 22:44:11 +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: 8bit Subject: Re: [PHP-DEV] Brainstorming idea: inline syntax for lexical (captured) variables From: rowan.collins@gmail.com (Rowan Tommins) On 14/03/2023 21:23, Ilija Tovilo wrote: > One thing to note is that, as I've learned recently, anonymous classes > can actually be instantiated at a later point with some tricks. > > https://3v4l.org/2OcmP Huh, that's freaky... I guess this is all a reminder that these really are anonymous *classes*, not anonymous *instances* - at some point, a class entry needs to be generated. I guess you'd have to generate a new class entry every time the "new class" line was run, and inject the extra values into that. If it was limited to capturing scalars and arrays, you could treat it as a kind of macro expansion, i.e. this ... $example = new class {     public $inner = $^outer; } ... could be a sort of sugar for: eval(     sprintf(         'return new class {             public $inner = %s;         };',         var_export($outer, true)     ) ); Which is valid code, if not particularly efficient: https://3v4l.org/sQaUS That doesn't allow for normal object semantics, though, so is probably a non-starter. > This could also prove technically challenging. Currently, property > defaults are constant ASTs and unique per class (not object). As I understand it, the main reason object properties weren't included in the "new in initializers" RFC was the problem of when side effects would occur: https://wiki.php.net/rfc/new_in_initializers#unsupported_positions That's not an issue here, because we're not *creating* an object, we're "just" resolving the $^outer token to an existing zval, and storing it in the default property table. It might actually be easier to *only* allow capture into property initialisers: 1. Create a new class entry without any property default (or clone a pre-compiled base) 2. Add the captured values to the default property table 3. Create an instance, run its constructor, etc, as currently Or even: 1. Use the existing logic to create an anonymous class entry ignoring the capturing syntax 2. Create an instance, initialise normal defaults, but don't run the constructor yet 3. Push the captured values directly into the *instance* properties 4. Run the constructor I'm probably in way over my head here, so I should probably stop here, in the hope that it will inspire someone more knowledgeable to come up with something workable, because I'd really love for anonymous classes to be more flexible than they are. Regards, -- Rowan Tommins [IMSoP]