On 24/03/2021 21:00, Rowan Tommins wrote:
As Christian says, automatic capture is a dramatic change to the language's scoping rules, and IMHO requires a more thorough justification on why the current syntax is burdensome. As I've said previously, my naive impression is that a long list of captured variables ought to be as bad a code smell as a long list of parameters; I'm willing to be proven wrong on this, but nobody has yet stepped forward with a real-life example.

Automatic capture ceased to be a dramatic change from PHP the day after short closurers were introduced. So they've been a part of PHP for years now.

I hit long lists of use() repeatedly, almost always because I need to perform a series of operations within a callback, prime examples are database transactions and using throw-aware buffering handlers.

In general, anything that needs to wrap a code block in a complex set of instructions before and after.

To give my own example, earlier this week I wrote the following:

$x = function () use ($to, $library, $thread, $author, $title, $library_name, $top_post) { ... }

That was just to get those variables inside a callback that could be invoked inside a throw-aware buffering helper.

I believe that by explicitly stating my intent to use auto capture by using fn() { ... } that my code would have been cleaner with less noise.

If I thought otherwise, I would be under no obligation to use them and could use function() { ... } instead.

For me auto capture is a solid +1.

Mark Randall

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php

Reply via email to