Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112001 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 44246 invoked from network); 5 Oct 2020 10:47:24 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 5 Oct 2020 10:47:24 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 04E501804C6 for ; Mon, 5 Oct 2020 03:00:31 -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,HTML_MESSAGE, SPF_HELO_NONE,SPF_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-mahalux.mvorisek.com (mail-mahalux.mvorisek.com [77.93.195.127]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Mon, 5 Oct 2020 03:00:29 -0700 (PDT) Received: from 5e967a9fd132 (10.228.0.215) by mail-mahalux.mvorisek.com (10.228.0.4) with Microsoft SMTP Server (TLS); Mon, 5 Oct 2020 12:00:27 +0200 MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="=_139aca2ea7502713ed773f90915652da" Date: Mon, 05 Oct 2020 12:00:26 +0200 To: Andreas Leathley Cc: internals@lists.php.net In-Reply-To: <881e2335-5f81-0d79-6a17-06bc20f14223@gmx.net> References: <446c9894-191f-e53b-4534-31c4e7b91d7b@gmail.com> <881e2335-5f81-0d79-6a17-06bc20f14223@gmx.net> Message-ID: <7e50dba8b8749b9e32c803d7ded20fab84e21dc74ec9a55d8127e961bb036cd8@mahalux.com> X-Mailer: SAP NetWeaver 7.03 Subject: Re: [PHP-DEV] RFC: Support for multi-line arrow functions From: vorismi3@fel.cvut.cz (=?UTF-8?Q?Michael_Vo=C5=99=C3=AD=C5=A1ek_-_=C4=8CVUT_FEL?=) --=_139aca2ea7502713ed773f90915652da Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=UTF-8; format=flowed Yes, "use (*)" is perfect! With kind regards / Mit freundlichen Grüßen / S přátelským pozdravem, Michael Voříšek On 5 Oct 2020 11:57, Andreas Leathley wrote: > On 04.10.20 22:08, Rowan Tommins wrote: > >> If we added an opt-in syntax for "capture everything", we might >> instead write this: >> >> $f = function() use (*) { >> $x = $y = $z = null; >> } >> >> Without re-initialising all local variables, we would no longer be >> able to know if they were actually local without looking at the >> surrounding scope for a value that might be captured. I am unconvinced >> by this trade-off of opt-out instead of opt-in. >> >> One use case I've seen proposed is closures which capture a large >> number of variables; I would be interested to see an example where >> this is the case and is not a "code smell" in the same way as >> requiring a large number of parameters. > > Something like "use (*)" seems like a great enhancement to me. I often > use a wrapper function for SQL transactions, something like: > > ```php > public function update(int $numberId, int $addressId, bool $isMainNumber > = false): void > { > $this->transaction->run(function () use ($numberId, $addressId, > $isMainNumber): void { > // Do all SQL queries for the update > }); > } > > ``` > > In these cases there is a lot of redundancy because of having to import > the variables, and if a variable is added, it has to be added in two > places in a slightly different way. The following would be much nicer: > > ```php > public function update(int $numberId, int $addressId, bool $isMainNumber > = false): void > { > $this->transaction->run(function () use (*): void { > // Do all SQL queries for the update > }); > } > > ``` > > This would also increase code readability. > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php --=_139aca2ea7502713ed773f90915652da--