This RFC proposes a way to have multi-line short closures — closures that don't need explicit
use statements, but can still have multiple lines and a return statement.
At least once a week, I throw away an array_map because it ended up looking too bloated and go with a classic foreach instead. Short Closures 2.0 without the use(...) block would've solved this problem, just 2 votes...
The 'use' statement clarifies the scope for me. So a proposal like this could have the side effect of mixing scope which would lead to a confusing code.
We spend a lot more time reading code than writing it. The elegance of short closure combined with the convenience of variable scope usage has already shown to be a game changer on Typescript and there doesn’t seem to be any technical issue with having it on PHP.
I really do not want to see this in PHP because it tempts to apply closures on everything. The question is not if we can but if we should.
PHP is evolving. There are new concepts added to many programming languages to ease writing and reading (more important!). PHP should focus more on developer experience (but not for legacy projects that get never upgraded to PHP 8+).
mmh, seems to me that just replacing function by fn and having such a different behaviour is risky
It improves the readability and usability of closures that consist of more than one assignment.
Auto capture by-value is clear enough with a single expression closure, but I think for multiple statement functions it could be confusing to have to keep track of scope. Being explicit (with
use()) makes it simple and concise.
Makes code more clean because it is shorter and the
use keyword is not needed anymore.
Short closures were meant to be that - short. If you need multiple lines, just create a function (it can even be a closure). Or just use a standard anonymous function instead of a short closure. It will also create confusion with having access to variables outside the short closure scope while letting it look like a normal function.
Just look at how other languages solve this. C# for example. Make short closures like that.
Would avoid me having to use dodgy looking code such as
fn($item) => [$this->doSomething($item), $this->doSomethingElse($item)]
I really love short closure. JS solve this
Since we have already autocaptures for one liners there should be also option for multiple liners.
A breakthrough for language, easy to write easy to read.