Re: RFC: Support for multi-line arrow functions

From: Date: Mon, 05 Oct 2020 10:00:26 +0000
Subject: Re: RFC: Support for multi-line arrow functions
References: 1 2 3  Groups: php.internals 
Request: Send a blank email to internals+get-112001@lists.php.net to get a copy of this message
Yes, "use (*)" is perfect! With kind regards / Mit freundlichen Grüßen / S přátelským pozdravem, Michael Voříšek On 5 Oct 2020 11:57, Andreas Leathley wrote:
On 04.10.20 22:08, Rowan Tommins wrote:
If we added an opt-in syntax for "capture everything", we might instead write this: $f = function() use (*) { $x = $y = $z = null; } Without re-initialising all local variables, we would no longer be able to know if they were actually local without looking at the surrounding scope for a value that might be captured. I am unconvinced by this trade-off of opt-out instead of opt-in. One use case I've seen proposed is closures which capture a large number of variables; I would be interested to see an example where this is the case and is not a "code smell" in the same way as requiring a large number of parameters.
Something like "use (*)" seems like a great enhancement to me. I often use a wrapper function for SQL transactions, something like:
public function update(int $numberId, int $addressId, bool $isMainNumber
= false): void
{
$this->transaction->run(function () use ($numberId, $addressId,
$isMainNumber): void {
// Do all SQL queries for the update
});
}

In these cases there is a lot of redundancy because of having to import the variables, and if a variable is added, it has to be added in two places in a slightly different way. The following would be much nicer:
public function update(int $numberId, int $addressId, bool $isMainNumber
= false): void
{
$this->transaction->run(function () use (*): void {
// Do all SQL queries for the update
});
}

This would also increase code readability. -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: https://www.php.net/unsub.php


« previous php.internals (#112001) next »