On 2025-04-01 09:38, Ilija Tovilo wrote:
> Hi everyone
>
>
> This is essentially already possible through a by-reference capture.
>
> $fibonacci = function (int $n) use (&$fibonacci) { ... };
>
> This cannot be a by-value capture because by the time the closure is
> created and variables are bound, $fibonacci is not assigned yet. The
> downside of by-reference capturing is that if $fibonacci is
> accidentally reassigned, the reference within the closure is also
> changed.
>
> However, I consider a language change largely unnecessary. Instead,
> this may be solved with a very simple static function:
> Closure::getCurrent().
>

How about mutual recursion, where two of these functions call each other? Currently one can write

$left = function(array $a) use(&$second) { ... };
$second = function(array $a) use($first) { ... };

(The second can capture the first by value, but the first still needs to capture the second by reference.)

With Closure::getCurrent(), how does either function reference the other?

Reply via email to