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