Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112002 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 45921 invoked from network); 5 Oct 2020 10:55:49 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 5 Oct 2020 10:55:49 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id CAE201804C0 for ; Mon, 5 Oct 2020 03:08: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,HTML_MESSAGE, 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-il1-f178.google.com (mail-il1-f178.google.com [209.85.166.178]) (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 ; Mon, 5 Oct 2020 03:08:53 -0700 (PDT) Received: by mail-il1-f178.google.com with SMTP id o18so2368846ill.2 for ; Mon, 05 Oct 2020 03:08:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=nm4SxB3Gf3aqpV13tLAizSVLovIjh06f3xoDqT58TjQ=; b=XCPkCk6LymoCCtJdPFul5AS/VHARGAH/wZPSchbbMflQG4SJmCqF7EIr3w4+hxrh4F QueY7+nnTkcMe600mF5ZfDXgwDk0oaDQc4hL5/E2gfd6pBKswZOAuXIzsrhFG8nScpSQ UVoX7iywiMm0p+hNYQuM28PkIHauDkf5PLa+BSfwsAVr39MZyLCkANqIINdbULxfa/BC CnOt1e/QwCkE75JYytKzVr5YzcMLBKrvaNhyDu6J/fNHdWFvIBYDNlmzEjjm9gu7xVmL JS5Eh+bG45fLiHRjQFSPpdFhRtw0cD7kZydZSkaXA7d7W7RGFarBK2dXAc/JZ1udqzxU fuGw== 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; bh=nm4SxB3Gf3aqpV13tLAizSVLovIjh06f3xoDqT58TjQ=; b=WcD5bv8CHLSJEsn6zJvjiNF2yu2KgY2D5XGD8B465JjNAn0XT9vamW85MaAlJ7RIXk V/riwNScK9d/TEIiRW7wPGgvthACXYrtKb9vy9Lmu/6i5b6LPFrlQzOrjyzbfk4Fzk2l j+E0L05cIBzmGIyXSSnqPe5nCwpkxHAaFshCJXDGNti4Low027MWLPNohqjsjK1Yt9la PUrm3CsapKWRRjdqxjcoYlM5FY15w5D0gXsXRfZquDgrendcTacHBDi6rPAC42KiqbXK vO6EiZrgykPeVbYK59BzD7GAXYt0pqMGjdr8FfPIBHauAM+bcEjd8xAc5mjJTx2Wx9Ad Kuww== X-Gm-Message-State: AOAM533A7sXYIs1ZXaDfkniNqfUCmClx01s7d3lq4wFULR4yvyXFFgjT XeVrEf656KkLiEoGLIc/IOkkTVcfTXgbgShRqWI= X-Google-Smtp-Source: ABdhPJzuTS4/ZlvCQVjLyceegUhTyxqrx5+/ShxWGS+ARAOEWnCMP6xAYiAnNP93InnI1okZg0uSa67YNU4VaX03ZDU= X-Received: by 2002:a92:3501:: with SMTP id c1mr11226348ila.146.1601892532502; Mon, 05 Oct 2020 03:08:52 -0700 (PDT) MIME-Version: 1.0 References: <446c9894-191f-e53b-4534-31c4e7b91d7b@gmail.com> <881e2335-5f81-0d79-6a17-06bc20f14223@gmx.net> <7e50dba8b8749b9e32c803d7ded20fab84e21dc74ec9a55d8127e961bb036cd8@mahalux.com> In-Reply-To: <7e50dba8b8749b9e32c803d7ded20fab84e21dc74ec9a55d8127e961bb036cd8@mahalux.com> Date: Mon, 5 Oct 2020 12:08:26 +0200 Message-ID: To: =?UTF-8?B?TWljaGFlbCBWb8WZw63FoWVrIC0gxIxWVVQgRkVM?= Cc: Andreas Leathley , PHP internals Content-Type: multipart/alternative; boundary="0000000000005e464b05b0e9ab82" Subject: Re: [PHP-DEV] RFC: Support for multi-line arrow functions From: kjarli@gmail.com (Lynn) --0000000000005e464b05b0e9ab82 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Mon, Oct 5, 2020 at 12:00 PM Michael Vo=C5=99=C3=AD=C5=A1ek - =C4=8CVUT = FEL < vorismi3@fel.cvut.cz> wrote: > Yes, "use (*)" is perfect! > > With kind regards / Mit freundlichen Gr=C3=BC=C3=9Fen / S p=C5=99=C3=A1te= lsk=C3=BDm pozdravem, > > Michael Vo=C5=99=C3=AD=C5=A1ek > > 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 =3D function() use (*) { > >> $x =3D $y =3D $z =3D 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 $isMainNumbe= r > > =3D 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 $isMainNumbe= r > > =3D 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 How should php deal with the scenario where you want to `use` everything and have one variable by reference? ``` function () use (*, &$butNotThisOne) {}; ``` --0000000000005e464b05b0e9ab82--