Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:113764 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 789 invoked from network); 25 Mar 2021 12:48:31 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 25 Mar 2021 12:48:31 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id C34E11804C0 for ; Thu, 25 Mar 2021 05:44: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=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-qk1-f174.google.com (mail-qk1-f174.google.com [209.85.222.174]) (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 05:44:29 -0700 (PDT) Received: by mail-qk1-f174.google.com with SMTP id z10so1505944qkz.13 for ; Thu, 25 Mar 2021 05:44:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=newclarity-net.20150623.gappssmtp.com; s=20150623; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=mdFP2JaQ9v8x3rzehMTpOJrO3PbUG0EIzlhfFy78NwY=; b=SXodsn+FjJn6+hgTJj5rU5wLxFE53mrfpm3hyR4HStjONyEL4l2eHP6uKDYQy7TyS6 3KPFSvWjVNYhHUQfpU1BYSd3bCDOVGFByyFWn2YmwEMTPITAb1sS/bD0L+QSMGCWtMy7 1lGGnjgEp/FCqo3W1yKrcfx9ugIy1MsL2nydcrs9AewP0tT0Wb0PUK2EXZFpkE8oB91N 0RVcccV6IJNr38Kqq6yKJuSsgaRNeab0VU/al8omMg3EqPVQrxDIfK6JAcS+TgLZCoBC 6WViAtXch30PR0R4C1karC3u3Dr4mEeyfGn9Wu/dhzXm6S63nD2seZGG4MDZ/z/4Yy20 SGUA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=mdFP2JaQ9v8x3rzehMTpOJrO3PbUG0EIzlhfFy78NwY=; b=KrewN6wh0x7CR/hql/18ff6D/zev71yZd8qJkuNIbw1OjC7M7mR34qO4pic7H6zna8 Q2tMFtsW0yqkjn5FcWDiL1bMdwe0wptuERhRySeGwdZ7s5RCCqzLaKYcjDjdfmp9LUqd /B9qGUvZGXon1XiPA2jP2Irxt8Js4cxiUQg1hJazo5aVgdmca6uO895eloY+b2gnXVFv BdczDiyDgRSCR2jxkdqWXfzySkSlpJT/qVNVLzoRRvcEQu7csu2ZplQQPBOYF9ntbfwt dbQnmSd0kt6YFF7F5CXxLgGF/FdhKrwzhfVHhvo9qkdOiRIson0Vy9JI9EQtUTgAqsVs rSlg== X-Gm-Message-State: AOAM533GPy0YusduBEXbxUf/Sfn+rFw8Rh623Sb1TTzJ/FKSfDkPgFCQ W8fzWnu394L1k42Xpk51zOeLwg== X-Google-Smtp-Source: ABdhPJwNwseEgVlbKyQnwtgDu7IqMwZY9xZADY1pqcQtyDdyqrvuTea/83DHKb/WoOmjPmQFbjIpxA== X-Received: by 2002:a05:620a:6c1:: with SMTP id 1mr8199059qky.198.1616676266913; Thu, 25 Mar 2021 05:44:26 -0700 (PDT) Received: from [192.168.1.239] (c-24-98-254-8.hsd1.ga.comcast.net. [24.98.254.8]) by smtp.gmail.com with ESMTPSA id v7sm3341736qtw.51.2021.03.25.05.44.25 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 25 Mar 2021 05:44:26 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 13.4 \(3608.120.23.2.4\)) In-Reply-To: <734f12de-da98-6b76-c2fe-8682f4d177aa@gmail.com> Date: Thu, 25 Mar 2021 08:44:25 -0400 Cc: internals@lists.php.net Content-Transfer-Encoding: quoted-printable Message-ID: 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> To: Rowan Tommins X-Mailer: Apple Mail (2.3608.120.23.2.4) Subject: Re: [PHP-DEV] [RFC] Auto-capture multi-line closures and shortfunctions take 2 From: mike@newclarity.net (Mike Schinkel) > On Mar 25, 2021, at 5:28 AM, Rowan Tommins = wrote: >=20 > On 25/03/2021 04:12, Mike Schinkel wrote: >=20 >> Actually, there is a way to declare a local variable. Use the long = function syntax instead as there is no proposal to deprecate it. >=20 > That's not quite what I meant. I meant that you can't say "capture by = default, but this variable is definitely local". Yes I can see you want a way to declare a local in an auto-capturing = fn(), but respectfully I do not see why using the function() syntax is = not a viable alternate solution when and if you want to explicitly not = capture a variable. I said "want" because you will never *need* to as you can always use a = variable name that is not in the outer scope.=20 And yes I know, someone may come along later and add a named variable in = the outer scope that matches your implicit local variable's name and = potentially break your code, but again if that is a concern why can't = you just use function() instead of fn()? And wouldn't they actually be = breaking their code (the outer code) if they did this? Respectfully, you seem to be calling out a problem for which there is a = tailor-designed solution explicitly recommended in the RFC. How can = function() not address your stated concern? > I'm guessing maybe "$foo =3D 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. >=20 >=20 >> Instead of using array_filter() with a side-effect free closure, I = use a for loop because it is easier to reason about visually. >=20 > Is that a bad thing? In many cases it's probably more efficient, and = easier to read. Depends on what your priorities are. If you priorities are coding in = the absolute most efficient manner and/or with the most clarity, no it = is probably not a bad thing. But if your priorities are coding in the most robust manner where your = code is free from side-effects by design =E2=80=94 in a functional style = =E2=80=94 and the performance difference is insignificant, then yes it = is a bad thing.=20 And at least one of the pair of RFCs spell out this motivation to allow = for more functional style explicitly.=20 >> 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? >=20 > 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*. Respectfully again, to argue a use-case could be addressed in a = different way than proposed by the RFC when that different way uses a = hypothetical language feature that AFAIK does not have an RFC currently = under consideration rings hollow to me. Minimally I would expect you to at least first float the hypothetical on = the list to see if there is appetite for it rather than just using is = idea to snipe at an RFC that many people appear to support. > You mentioned: >=20 > > prime examples are database transactions and using throw-aware = buffering handlers. Actually, I did not provide that example. Other people did. =20 What I stated was I had other use-cases that concerned me more than = transactions, which I already explained in my email.=20 However, in no way do I mean to argue against their value for other = developers who mentioned them. > If your current code looks something like this: >=20 > 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); > }); >=20 > That's a perfect use case for a Context Manager: >=20 > with ( wrap_in_buffer() ) { > doSomething($to, $library, $thread); > andThenSomethingElse($author, $title, $library_name, $top_post); > } >=20 > 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. But thanks for clarifying with examples, because I did ask for that. That said, "with" is still hypothetical. Are you planning to champion = its inclusion into PHP with a discussion and then follow up RFC? -Mike=