Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:113761 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 84161 invoked from network); 25 Mar 2021 09:32:30 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 25 Mar 2021 09:32:30 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id C70F91804DD for ; Thu, 25 Mar 2021 02:28:29 -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,NICE_REPLY_A, 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-ed1-f47.google.com (mail-ed1-f47.google.com [209.85.208.47]) (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 02:28:29 -0700 (PDT) Received: by mail-ed1-f47.google.com with SMTP id h13so1618375eds.5 for ; Thu, 25 Mar 2021 02:28:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding:content-language; bh=HiBjIIFRbD0rWlyN8EtaEpbnjih1jP/dYCSAeKzgj04=; b=dTwpM+NTyAwc0rpPZY/5e5MyszGaPE+Wi16jg4Q4yiaS85tSgC0k8FvE4lZOPEwIi5 4oJgvvtbtEvxOcIw2lHoW5eBbU3BZeggvljaJbNY2mwsW1NoBXJmBwDAXk64mlzdBeo7 z1gIAHnYrmjLwas8vvXJSIc90p4on/dN+gaKqVeYliqdlt8YNceCbfVgu1joEYjU/pBx Lm4A1ctvyHZZ7I4Zg+dGkbHhvq7FlkgEYVg+jt5dXvE7E8xde7XL2gCcjFprtC9A8ch7 nRALcyv8OKE1ODFKSqx37HbnVN3qwTsOyDehq+78j58wxyV5W9gh4/CJdhS/HiRFUDuz mz5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=HiBjIIFRbD0rWlyN8EtaEpbnjih1jP/dYCSAeKzgj04=; b=CLXyVeljyXCCZDBqSlfiPKY6/roYuikrViRfpDmaM9A5uTLeHBd5ZUPqo7dUCmQxB0 Qj0Awy/14hTtU6ZfenAGoZco5hGqmiSlIf+est3Tb6LWrTOZcBEJHmyx5yEaZEbt5Ync IH0HsQ8h6U6NcL2XzatStSwLep/qILrLnd67qvXC0fiMoCP0outCfYFbuWDL0x7f2az9 ht606qUTTASD3Z9x4TvQLMh3TaLyMRIRsL3g+ZcHbdzSYfw+BLJ4U15bWjvGPO69D0pN 0oaROM3L4ahO6lFa/QmRPCIXB4IgayCKuKlc76ZxeruX6dTw4aJe9X+j0tYqDf06MHsR x6Bg== X-Gm-Message-State: AOAM530jiygEahCcB8VFpC/ORvd+rdyK6Mr0NeOz67hrhtmiIN59qA+O 3unvTHIUhdDosZDSaGio+QD2FeLkMlQ= X-Google-Smtp-Source: ABdhPJyqWAYH2JsPh49iklaHAzp2HNmlAc5WMfDrmUp2ijv3GJIueA9xyO0xqUKrth8W4SM767HUZA== X-Received: by 2002:aa7:d0c2:: with SMTP id u2mr7933069edo.158.1616664508062; Thu, 25 Mar 2021 02:28:28 -0700 (PDT) Received: from [192.168.0.22] (cpc104104-brig22-2-0-cust548.3-3.cable.virginm.net. [82.10.58.37]) by smtp.googlemail.com with ESMTPSA id c17sm2457819edw.32.2021.03.25.02.28.27 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 25 Mar 2021 02:28:27 -0700 (PDT) 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> <605bae82.1c69fb81.f49f7.d11eSMTPIN_ADDED_MISSING@mx.google.com> <919e30e7-3e5e-d955-7bb4-1e1b5825cdd1@gmail.com> <635DD146-FC6F-4991-8D2C-5A6B492722D5@newclarity.net> Message-ID: <734f12de-da98-6b76-c2fe-8682f4d177aa@gmail.com> Date: Thu, 25 Mar 2021 09:28:27 +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: <635DD146-FC6F-4991-8D2C-5A6B492722D5@newclarity.net> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-GB Subject: Re: [PHP-DEV] [RFC] Auto-capture multi-line closures and shortfunctions take 2 From: rowan.collins@gmail.com (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]