Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:113766 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 4803 invoked from network); 25 Mar 2021 13:16:54 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 25 Mar 2021 13:16:54 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 11C7F1804C0 for ; Thu, 25 Mar 2021 06:12:56 -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, 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-lj1-f182.google.com (mail-lj1-f182.google.com [209.85.208.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 ; Thu, 25 Mar 2021 06:12:55 -0700 (PDT) Received: by mail-lj1-f182.google.com with SMTP id u20so2989288lja.13 for ; Thu, 25 Mar 2021 06:12:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to; bh=lvRBIlBGRPnBbEdDS9XUyEcGMSesH4ZXt4M02qE0BvU=; b=QeozPpGnVFpS7dkfJqFVrBGMPqCMKdJ1rSZ9Mk1yriAuZ2TswQCmIUZWa3qQ9k8eJ6 fqipV9YFTA7P8IYppez6PJ8fDoOZs13vOq0Qgvbaaxk2kby8vrKspGoiXbuihDP1nbbV Up86o7zIbBEQmefnwsM6SeNoeVGziCMY2DPgnR6WoIxLtH7vcmudZAVtLbOqLPX4xxb3 X76tmJdMpQPMvypC7/HWXRL1W9W8zEg+g0atHSS3BU3aflSmK+m4bR/mKKuxT57Cy56S F9v9TqULmeDjGPRK43Q81U1w7+kemiTumUeOX7sdgIE6QWSO2GIZMo5n68t9c0rlXnIC hJ3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to; bh=lvRBIlBGRPnBbEdDS9XUyEcGMSesH4ZXt4M02qE0BvU=; b=o2Wxzq8UC6jVUzNdOezCnmE2o4dBZnsxB5eMPW2Xggn/kl1uDdWeO14b2IVa/WF/db yVFFFsYiAjDExaCZd5ClM2r+DmSUHBqoJQWtjhmeON4EpId4dqxA7BNgxhiHsrfPGM0w 8hIlGvHIm6qFkXQutphgvLCubRslhQffZPU5TXbrIdNgbQZJ+KtL8Q253nJ+KwYqAFtM oFJbtkYIYjLMRym9uD0OQYBblXoOx46LOaE1sjN+0RBsqP2IO1rFmw5MzWWK8JbNfQI6 5+Y7qZeu4R+9zWHFAfwz4RJDAB2ai4Go637x4SGUvPozxFxhCg/aebY7va2fbOaZ8kca noqA== X-Gm-Message-State: AOAM530QxIhFm9sTaTJQa75n18CS1WFxuvHHUm/NlDTXmIosjBoH9IPz a4WvxXpEqYHAsPxbODX8GlQ4o8fbj2DY9iJkZA/qMHX3 X-Google-Smtp-Source: ABdhPJy7vel9uTDIByGbhAhBaKuxZe+JoruZ5jPvw9t4zIEunBT8R/x5NksgnHMMISuVfpUKO6AiQMmQ9pVpqFi9nFk= X-Received: by 2002:a2e:86d0:: with SMTP id n16mr5842963ljj.288.1616677974300; Thu, 25 Mar 2021 06:12:54 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:ab3:5f8f:0:0:0:0:0 with HTTP; Thu, 25 Mar 2021 06:12:53 -0700 (PDT) In-Reply-To: <734f12de-da98-6b76-c2fe-8682f4d177aa@gmail.com> References: <88c9eb5f-f80c-4869-b7f8-1b58b9e2eaa3@www.fastmail.com> <4DC3B66E-A91A-4AA9-8872-8EE9DE92C2D4@cschneid.com> <8c72c162-83c0-7c7f-2fa7-4fbe3fb30a4a@gmail.com> <605bae82.1c69fb81.f49f7.d11eSMTPIN_ADDED_MISSING@mx.google.com> <919e30e7-3e5e-d955-7bb4-1e1b5825cdd1@gmail.com> <635DD146-FC6F-4991-8D2C-5A6B492722D5@newclarity.net> <734f12de-da98-6b76-c2fe-8682f4d177aa@gmail.com> Date: Thu, 25 Mar 2021 14:12:53 +0100 Message-ID: To: internals@lists.php.net Content-Type: text/plain; charset="UTF-8" Subject: Re: [PHP-DEV] [RFC] Auto-capture multi-line closures and shortfunctions take 2 From: olleharstedt@gmail.com (=?UTF-8?Q?Olle_H=C3=A4rstedt?=) I feel like some of these examples would be better covered by command object pattern and data-transaction objects, instead of FP idioms. wrap_in_buffer pretty clearly so. 2021-03-25 10:28 GMT+01:00, Rowan Tommins : > On 25/03/2021 04:12, Mike Schinkel wrote: > >> Actually, there is a way to declare a local variable. Use the long >> function syntax instead as there is no proposal to deprecate it. > > That's not quite what I meant. I meant that you can't say "capture by > default, but this variable is definitely local". > > I'm guessing maybe "$foo = null;" at the top of the function would stop > it auto-capturing, but the semantics of what exactly gets captured > aren't spelled out in the RFC. That really needs to be added IMO. > > >> Instead of using array_filter() with a side-effect free closure, I use a >> for loop because it is easier to reason about visually. > > > Is that a bad thing? In many cases it's probably more efficient, and > easier to read. > > > > >> It is not clear to me from reading the PEP how a "with" statement obviates >> the benefit of variable auto-capture? Maybe because I don't "think" in >> Python, so from reading their examples I cannot see how this relates? > > > I didn't mean to say that the "with" statement would replace closures in > general, but it would make them unnecessary *for some specific use > cases*. You mentioned: > > > prime examples are database transactions and using throw-aware > buffering handlers. > > If your current code looks something like this: > > wrap_in_buffer(function($transaction) use ($to, $library, $thread, > $author, $title, $library_name, $top_post) { > doSomething($to, $library, $thread); > andThenSomethingElse($author, $title, $library_name, $top_post); > }); > > That's a perfect use case for a Context Manager: > > with ( wrap_in_buffer() ) { > doSomething($to, $library, $thread); > andThenSomethingElse($author, $title, $library_name, $top_post); > } > > There's no callback, so no need to capture anything, and the > implementation of all the try-catch-finally logic would be baked into > the language, so the implementation of wrap_in_buffer() would also be > much simpler. > > > Regards, > > -- > Rowan Tommins > [IMSoP] > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php > >