Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:113747 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 29153 invoked from network); 24 Mar 2021 21:30:31 -0000 Received: from unknown (HELO localhost.localdomain) (76.75.200.58) by pb1.pair.com with SMTP; 24 Mar 2021 21:30:31 -0000 To: internals@lists.php.net References: <88c9eb5f-f80c-4869-b7f8-1b58b9e2eaa3@www.fastmail.com> <4DC3B66E-A91A-4AA9-8872-8EE9DE92C2D4@cschneid.com> <8c72c162-83c0-7c7f-2fa7-4fbe3fb30a4a@gmail.com> Date: Wed, 24 Mar 2021 21:26:18 +0000 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.8.1 MIME-Version: 1.0 In-Reply-To: <8c72c162-83c0-7c7f-2fa7-4fbe3fb30a4a@gmail.com> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Language: en-GB Content-Transfer-Encoding: 7bit X-Posted-By: 87.81.129.66 Subject: Re: [PHP-DEV] [RFC] Auto-capture multi-line closures and shortfunctions take 2 From: marandall@php.net (Mark Randall) Message-ID: 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