On 14/11/2021 19:49, Côme Chilliet wrote:
Le 14 novembre 2021 18:13:18 GMT+01:00, Nikita Popov <nikita....@gmail.com> a 
écrit :
I have ultimately decided to withdraw this proposal.
This is sad to hear, because this is a really big footgun, which has hit me in 
the past. I ended up adding a codestyle rule forcing byref foreach to be 
followed by an unset to avoid this.

I understand making a special case for one syntax and not the other is 
unsatisfying but I was hoping for an other outcome.


I think I agree with both of you - I agree that this is a problem worth solving, but also that the proposed solution isn't quite the right one.

For one thing, it's just too "magic": although the current behaviour is *surprising*, it's actually a consequence of very straight-forward language rules; the proposed solution introduced a special case that isn't particular easy to explain, with extra edge cases to watch out for.

One alternative solution would be to introduce block-scoped variables, which most of the time is what people want (and expect) in *any* foreach loop. Then the special-case for by-ref unsetting goes away, e.g.:

foreach ( $array as let $x ) {
    var_dump($x);
}
var_dump($x); //null / undefined

foreach ( $array as let &$x ) { // or "&let $x"?
    $x++;
}
var_dump($x); //null / undefined, not a reference

Regards,

--
Rowan Tommins
[IMSoP]

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php

Reply via email to