Re: RFC: Support for multi-line arrow functions
| From: | Sara Golemon | Date: | Fri, 30 Oct 2020 02:50:50 +0000 |
| Subject: | Re: RFC: Support for multi-line arrow functions | ||
| References: | 1 2 3 4 5 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-112147@lists.php.net to get a copy of this message | ||
On Mon, Oct 5, 2020 at 5:09 AM Lynn <kjarli@gmail.com> 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