On 6. 5. 2026 at 22:09:45, Bob Weinand <[email protected]> wrote:

> Volker and I drafted a RFC:
>

Hello, I’d like to offer my 2c as PHPStan maintainer. References are really
hard to wrap our head around. To precisely infer types of variables in a
closure that uses variables by reference, we’d have to check where it’s
called and how many times, then go back to the closure body and fill in how
the variables change throughout the lifecycle. When compared to analysing
normal code that doesn’t use references, this is a lot more complicated, I
might say even impossible in some cases.

So I’m not a fan of spreading these semantics in new language features.
Your Scope functions inherit variables from the outer scope as references
by default. Because the syntax looks just like arrow function (but with a
body that allows multiple statements), I’m opposed to the inconsistency in
behaviour: https://3v4l.org/YKu9K#v8.5.3

The references are also the reason why I’d be afraid to write any Scope
function in practice. Starting new scope, usually in a closure, but also in
a function, method, or property hook, should mean I have a clean slate and
I don’t have to review 1 000 lines of code that preceded that function. I
can assign new variables without having to worry about the impact to
surrounding code. This RFC violates that.

This RFC would allow to call functions like array_map with a Scope function
and modify variables that existed before the array_map call. I’d prefer if
people wrote a traditional foreach instead. I don’t think the examples in
the RFC are worth adding a new place references are used, especially hidden
like that, without using the “&” keyword, which is a warning sign about the
code doing something weird, and signalling we should be especially careful
when working with it.

Ondřej Mirtes

Reply via email to