Hi Larry, Arnaud,

On Mon, 13 Jun 2022 at 13:57, Arnaud Le Blanc <arnaud...@gmail.com> wrote:

>
> Auto-capture in PHP is by-value. This makes this impossible. It also makes
> explicit declarations non-necessary and much less useful.
>

Separating off some pedantism from the hopefully constructive comment,
I think some of the words in the RFC are a bit inaccurate:

> A by-value capture means that it is not possible to modify any variables from 
> the outer scope:

> Because variables are bound by-value, the confusing behaviors often 
> associated with closures do not exist.

> 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:

class Foo
{
  function __construct(public string $value) {}

  function __toString()
  {
     return $this->value;
  }
}

$a = new Foo('bar');
$f = fn() {
    $a->value = 'explicit scope is nice';
};

print $a; // prints "bar"
$f();
print $a; // prints 'explicit scope is nice';

Yes, I know you can avoid these types of problems by avoiding
mutability, and/or avoiding capturing variables that represent
services, but sometimes those things are needed.

When you are capturing objects that can have side effects, making that
capture be explicit is quite nice (imo). I think the different
emphasis on capturing scalar values or objects might come down to a
difference in style of how different people use closures.

cheers
Dan
Ack

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

Reply via email to