Re: Brainstorming idea: inline syntax for lexical (captured) variables
| From: | Rowan Tommins | Date: | Wed, 15 Mar 2023 07:38:35 +0000 |
| Subject: | Re: Brainstorming idea: inline syntax for lexical (captured) variables | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-119705@lists.php.net to get a copy of this message | ||
On 15 March 2023 03:28:39 GMT, "Alexandru Pătrănescu" <drealecs@gmail.com> wrote:
>How about first implementing use() for anonymous classes first?
>Something like:
>
>function foo(int $outer) {
> return new class() use($outer) {
> public function getIt() {
> return $outer;
> }
> };
>}
I think it's a lot less clear exactly where the variable is being imported to that way. Would
you still be able to overwrite $outer as a normal local variable in that function scope? Would it
return to its captured value every time the function runs? It's different from capturing into a
function, because you're not just adding a variable to a single existing scope.
Just allowing the captured var for property initialisation would be less ambiguous, but having to
list it in use() and then in the property list would be the same amount of code as you can currently
get to with constructor promotion (see my first example in the thread).
From what Ilija has pointed out, the implementation of actually getting the values into the right
place is going to be harder than handling the syntax anyway. My current feeling is that if it's
possible at all, it will end up as a shorthand for that constructor example: declaring normal
properties, and populating them as the instance is initialised.
Regards,
--
Rowan Tommins
[IMSoP]