Re: Brainstorming idea: inline syntax for lexical (captured) variables
| From: | Nicolas Grekas | Date: | Thu, 16 Mar 2023 09:28:07 +0000 |
| Subject: | Re: Brainstorming idea: inline syntax for lexical (captured) variables | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-119719@lists.php.net to get a copy of this message | ||
Hi Rowan,
I have been pondering for a while how to improve the anonymous class
> syntax to allow "capturing" of values from the outer scope, and came up
> with the idea of a special variable marker for "lexically captured
> variable" - instead of $foo, you would write $!foo or $^foo (I quite
> like the "upwardness" of $^).
>
To overcome the issues spotted in the thread, what about doing some sort of
CPP instead of autocapture?
new class (...$arguments) use ($outer) extends Foo {
public function getIt() {
return $this->outer;
}
}
This would be the equivalent of this:
new class ($outer, ...$arguments) extends Foo {
public function __construct(public mixed $outer, ...$arguments) {
parent::__construct(...$arguments);
}
public function getIt() {
return $this->outer;
}
}
And we could also allow this for better type expressivity:
new class (...$arguments) use (private int $outer) extends Foo {
// ...
}
Nicolas