Re: Brainstorming idea: inline syntax for lexical (captured) variables

From: Date: Wed, 15 Mar 2023 10:10:32 +0000
Subject: Re: Brainstorming idea: inline syntax for lexical (captured) variables
References: 1 2 3 4 5  Groups: php.internals 
Request: Send a blank email to internals+get-119706@lists.php.net to get a copy of this message
On Wed, Mar 15, 2023 at 8:38 AM Rowan Tommins <rowan.collins@gmail.com> wrote: > > 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] > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php > > The outer variable would be captured by value, and carried around with > the instance, like existing closures. For what its worth, there are ways to get references inside values: https://3v4l.org/5q93F Other than pointing that out, I like it.

« previous php.internals (#119706) next »