Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:113822 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 7581 invoked from network); 28 Mar 2021 09:29:56 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 28 Mar 2021 09:29:56 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 4C41F180503 for ; Sun, 28 Mar 2021 02:26:40 -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.4 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-pj1-f52.google.com (mail-pj1-f52.google.com [209.85.216.52]) (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 ; Sun, 28 Mar 2021 02:26:39 -0700 (PDT) Received: by mail-pj1-f52.google.com with SMTP id kr3-20020a17090b4903b02900c096fc01deso4505404pjb.4 for ; Sun, 28 Mar 2021 02:26:39 -0700 (PDT) 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:content-transfer-encoding; bh=CY5ieU2FVj4hrN0lse4FTbQOC1D8zn2SlCgMdSywygA=; b=ASMIkfVNesenYdIagX14keR3/Wr4VjyteXOqk6PIKLPYJ/yQmtOPNmvn/eDlRGLlmk WwGZB7yE+W/xZCldO+WfYAWHCaW8LrM1xl55UGh4qgZC5Y2WT20XXW4/kOJOyP3tc3hq 5xX2nIBr1slPUx/dQSD/KHAGXQnyMGsje+KESSpntpwoAu5OwTypLQkTEfAE09dFZf38 fMeoSXooZ4x5WXlD9x8gO6CI14ngTVDCO91FhNKS6Jihs5/WZnztQ4mDRhuqbqq23LBs xJ/cTwH/RjVz4oVg6LPdPvIKDeMKA4QrfQDFDEIvj1MJXWT3TS5UJ/RUX+i11bsnArc3 mWeA== X-Gm-Message-State: AOAM530E/jCLaz4lvdDxn4iUjQDcAClntArJfPsNBRZI67d26/Mr87Yf wcA6REbWg2qwG+3+9YMCgmzRoYtTQDAI2cY30HY= X-Google-Smtp-Source: ABdhPJxLAb3muv+tNtXMFe5aRJmsXM5JfTk/p/f+3Iz1UhoBDTUOL4Cpi2rfuuXFUHgD7J5TZkDnvmsFLzZuhb/W5B4= X-Received: by 2002:a17:90b:94c:: with SMTP id dw12mr21437914pjb.119.1616923593455; Sun, 28 Mar 2021 02:26:33 -0700 (PDT) MIME-Version: 1.0 References: <88c9eb5f-f80c-4869-b7f8-1b58b9e2eaa3@www.fastmail.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> <36E45DD6-E2BD-4801-BAAE-4355C83D1AC3@newclarity.net> <15AE4315-A456-4ED8-990A-49EBD76C5B46@newclarity.net> <5501FE70-FBED-47EB-8010-173644BC064F@newclarity.net> <3d453ce7-db16-fb75-91b4-9a2a71994164@gmail.com> In-Reply-To: <3d453ce7-db16-fb75-91b4-9a2a71994164@gmail.com> Date: Sun, 28 Mar 2021 12:26:22 +0300 Message-ID: To: Rowan Tommins Cc: PHP Internals Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] [RFC] Auto-capture multi-line closures and shortfunctions take 2 From: kalle@php.net (Kalle Sommer Nielsen) Hi Rowan Den l=C3=B8r. 27. mar. 2021 kl. 18.05 skrev Rowan Tommins : > Based on those, there seems to be no way to prevent a variable being > captured, even if its value is immediately discarded each time the > closure runs. This may be less than ideal: > > $results =3D getLargeReportFromDatabase(); > // ... > $fn =3D fn() { > $results =3D []; // coincidentally the same name, immediately > assigned its own value > // ... > } > unset($results); // does not free the array, because $fn has captured > the value I too am very concerned about this exact senario because it very easily overlooked. While I understand the appeal of having very short closures, I do think that such behavior needs to be explicit and would much rather prefer something like this: - Make the `fn` keyword an alias of the `function` keyword - Add a new `auto` (or similar) keyword for explicit auto capture Something like this: ```php return function () use ($v1, $v2, $v3, $v4) { /* ... */ }; ``` Would then be abled to be turned into: ```php return auto fn () { /* ... */ }; ``` That way it is very clear that the closure is intending on auto capturing at this point, at the same time it also allows people like me who prefers the `function` keyword to write. I personally don't think adding those 5 extra characters here for explicitity is bad, I would like explicitly over implicitit, I can see how many would simply just write `fn` without knowing there is a very vitual difference (similar to `static` closures is rare if you don't intend on binding `$this`). --=20 regards, Kalle Sommer Nielsen kalle@php.net