Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:113748 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 31830 invoked from network); 24 Mar 2021 21:50:08 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 24 Mar 2021 21:50:08 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id B264D1804D8 for ; Wed, 24 Mar 2021 14:45:59 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-vk1-f182.google.com (mail-vk1-f182.google.com [209.85.221.182]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 24 Mar 2021 14:45:59 -0700 (PDT) Received: by mail-vk1-f182.google.com with SMTP id u144so5800556vkb.13 for ; Wed, 24 Mar 2021 14:45:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=kIufDnLkB5P2xdTX37MzF5Bt6LNNRIaa2eAulci/LEc=; b=o5i8mC6kb1N4GbnzAKlRm5b9J/ZpJz4+ZjBke9etouS0hydTd17fUp3gnH5EFhqGdJ o3m3jQ178bwFukxQB27ZincF/6t0aI6pOAoUMc1V85UnYAaD7bLhLHozAFn+NEao6dsQ KffnZvJe8BbXhzEbHO9rvV5n2ADHrylDXfSpgn1U9W3CKGRrpycpCzRx5+Ggf/7xivHc 5Mp3qxy+giD8KDXMEMtuYKiQyzTIYiPUgLfYjKur071P1Ir+h6tvyT2kPwxYXefiOHp/ CNdguyICCVd6HJ8YCUwlItaJ4zZI31eH1o/KPGS0e3Wwd6nevwE9nTV8fIGnAo/qxQCn W0yQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=kIufDnLkB5P2xdTX37MzF5Bt6LNNRIaa2eAulci/LEc=; b=cu0EJw0qjjW/5y6obCzUcV063/hpbWuY5rxNzsceb7AvrBl45u+j9yxGZnf6LEHdOe Ks6tDbNuI6zYaFgcmvqc37EtH3Y8wntw15xBNzgnzWMamEQgiLBSpPfJwUaXMhIgi40h Zph+N7fQ6+l4lVZUAQ0n59uCt3MOgVCCbCUIWl7/Ezi4JzqO60RPeNoCtpmU+HwOYo4C PRcAVhYM2MOKLQx84CTT8kPNyGc7/IM7lcwnEY7m3As3aaybsAEI3TqRGJfS1c6sDQBs 3ciUgKCCCcwN1cnu9vVV4GJ7GVDZAbB6YNbptA5boQsOvRPC73Mo7aBBoKwLooCMAOZe 1ebQ== X-Gm-Message-State: AOAM531Bo31PlE2upN7XUYkTlNq9CUW1kG99ZnzEiG2a3n9tFpfuAYN0 OMEoy4+Vu3Ezgpr36UP7N9meXRKqREkC2GokQQft3IkdYo4= X-Google-Smtp-Source: ABdhPJxDp2t+bOXTr5tGg/4iKTGx3xVvLPV8kTsUoBPkQzD4MRBTFrsYHQPIei14MV2Ku9fqwf9Ua/O5j1+dnBStvhc= X-Received: by 2002:a1f:bf86:: with SMTP id p128mr3416115vkf.23.1616622356690; Wed, 24 Mar 2021 14:45:56 -0700 (PDT) MIME-Version: 1.0 References: <88c9eb5f-f80c-4869-b7f8-1b58b9e2eaa3@www.fastmail.com> <4DC3B66E-A91A-4AA9-8872-8EE9DE92C2D4@cschneid.com> <8c72c162-83c0-7c7f-2fa7-4fbe3fb30a4a@gmail.com> <605bae7f.1c69fb81.7d28d.c115SMTPIN_ADDED_MISSING@mx.google.com> In-Reply-To: <605bae7f.1c69fb81.7d28d.c115SMTPIN_ADDED_MISSING@mx.google.com> Date: Wed, 24 Mar 2021 17:45:45 -0400 Message-ID: To: Mark Randall Cc: PHP internals Content-Type: multipart/alternative; boundary="0000000000004e793805be4f39b0" Subject: Re: [PHP-DEV] [RFC] Auto-capture multi-line closures and shortfunctions take 2 From: chasepeeler@gmail.com (Chase Peeler) --0000000000004e793805be4f39b0 Content-Type: text/plain; charset="UTF-8" On Wed, Mar 24, 2021 at 5:26 PM Mark Randall wrote: > 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. > DB transaction is one place I have a long list of use parameters as well. Since the goal is to do as much before we start the transaction as possible, I always end up with a lot of variables resulting from that prep work that I need to utilize inside the transaction callback. My gut tells me the use-cases for long use lists that don't indicate code smell are pretty small though. > 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. > I agree. I think as long as we make it clear to users that function() does not provide auto capture, but fn() does, we're fine. In the same way that JavaScript make it clear that function(){} changes the meaning of "this" while () => {} does not. The fact that they behave differently like that is great in my opinion. I use function(){} when I want the changed context (like event listener callbacks) and () => {} when I know I only need access to the parent context and don't care about any possible redefinition. > > 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 > > -- Chase Peeler chasepeeler@gmail.com --0000000000004e793805be4f39b0--