> > > Because variables are captured by-value, Short Closures can not have
> > > unintended side effects.
> >
> > Those statements are true for scalar values. They are not true for objects:
>
> This is shown in the "No unintended side-effects" section of the RFC.

I'm confused by the last example:

    $fn2 = function () use (&$a) { /* code with $a AND $b */ }

Isn't that missing a ", $b" in the `use`?

And like others, I also find that allowing mixing explicit *by-value*
capture with auto-capture is not really needed and even confusing; if
you "expect that explicitly capturing by value will be rare in
practice" you might as well forbid it?

Maybe you don't even need to add explicit [by-reference] capture to
short closures at all, but rather extend *long* closures so that we
can write things like:

    $val1 = rand(); $val2 = rand(); $ref = null;
    $fn1 = function () use (...) { /* do something with $val1 and $val2 */ };
    $fn2 = function () use (&$ref, ...) { $ref = $val1 + $val2; };

(and even if not, at least mention in the RFC that it has been considered)?

By the way, what about *arrow* functions? e.g.

    $fn = fn () use (&$ref) => $ref = $val1 + $val2; // assigns and returns

Would that be allowed? Is it really *desirable*?

Regards,

--
Guilliam Xavier

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

Reply via email to