News for hackers. Stuff that matters.
Re:PHP 7.4 by dpcx on Thursday November 28, 2019 @09:36AM (#21657633)
The big thing enabled by them is not needing to `use ($variable)` to pull a variable in to the scope; however, IMHO, that actually then adds cognitive overhead to the system - I personally will stick to "fully" generated closures.
Re:PHP 7.4 by johannes1234321 on Thursday November 28, 2019 @11:13AM (#21658326)
This is a matter of the situation. For a single statement this can be extremely clear, if the function becomes longer being explicit is nice. And at some length the thing should have a name.

    usort($data, fn($l, $r) => $this->foo($l, $r));
vs.

    usort($data, function($l, $r) use ($this) { return $this->foo($l, $r); });
vs.

    usort($data, [$this, 'foo']);
Re:PHP 7.4 by deergomoo on Thursday November 28, 2019 @02:47PM (#21659747)
> For a single statement this can be extremely clear

PHP arrow functions can only contain a single statement and always implicitly return, so that sort of thing is exactly what they're designed for.

Although worth noting with your second example that you haven't needed to capture $this in closures since 5.4, and in fact it is an error to do so now (though I'm not sure when that was introduced).

Re:PHP 7.4 by johannes1234321 on Thursday November 28, 2019 @02:57PM (#21659811)
Using $this was just a simple example where all three cases could easily do the same. Make it $bar :)

Anyways: People are capable of writing loooooong expressions (chaining with && can be good to handle errors from multiple operations in one place (till you have to figure out which specific condition broke)) Thus "single expression" isn't neccisarily short ;)

Anyways: Point is "it can be useful, but developers should think, what they are doing" Perl's "there's more than one way to do it" probably goes too far, but being able to focus on the relevant parts and being able to providing the amount of context needed are good things.

Re:PHP 7.4 by leetbulb on Thursday November 28, 2019 @10:33AM (#21657978)
This is the correct answer. Having the option of implicit by-value scope binding is nice. However, I do agree that having the two does add to the overall cognitive cost, albeit not significantly.