On Mon, Oct 5, 2020 at 5:09 AM Lynn <[email protected]> wrote:
> How should php deal with the scenario where you want to `use` everything
> and have one variable by reference?
>
> ```
> function () use (*, &$butNotThisOne) {};
> ```
I would take a page out of C++'s book here. In C++ a closure is (some of
these bits can be omitted for brevity, but I'm not going to describe those
here as it's orthogonal to the topic):
[capture, vars](type argName) -> returnType {
statements;
return retVal;
}
Looking specifically at the capture vars section (in the example we are
capturing two variables, one named 'capture', one named 'vars'), there are
wildcards available as well:
[=](bool bval) -> void {}
The equal sign (as above) captures all variables used in the closure by
value.
Similarly, the ampersand `[&](int ival) -> void {}` captures all variables
used by reference.
Exceptions can be added to that list just as you suggested, so:
[=,&foo](double dval) -> double { return foo + dval; }
Or
[&,foo]() -> void { doSomething(foo); }
I think we could apply the same in PHP terms:
function(float $dval) use ($, &$foo) { return $foo + $dval; };
function() use (&$, $foo) { doSomething($foo); };
Plus or minor for parser convenience.
-Sara