Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112148 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 33829 invoked from network); 30 Oct 2020 18:47:27 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 30 Oct 2020 18:47:27 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 858A11804CC for ; Fri, 30 Oct 2020 11:06:54 -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, 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-wr1-f47.google.com (mail-wr1-f47.google.com [209.85.221.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 ; Fri, 30 Oct 2020 11:06:53 -0700 (PDT) Received: by mail-wr1-f47.google.com with SMTP id w14so7427358wrs.9 for ; Fri, 30 Oct 2020 11:06:53 -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=ayy/Y43IWnCQTSLM6yaVvA70ZVnaeYZHB37BEMMueeg=; b=hq/5w61QGdidVBaKT29LUlmrLZ+nzu4w1EJ54NiaDgNMsK45i1tGd4+E+8FU78swcE 2h9aKjAdd0LyvnwzcQanCeqz7fsWptTLCW66NfIXTVcsgfAD8YDqXHzTfylclkQLDh3v 9TYVfHtkb+qQQkAUjWMte4yr/WOdzVe3XDYfgzWJdzCKQ/fYEOtSufHxnNeA834/Ci+i yP03ZU9IhxaznBRb5euIvrM3bEOJDdOfma0KkK3AUI5AIhW8PMcsEbEM7IBr78/34kaz 0g0h8AcEGVLDCJep1Tk9rro+CWlMhR7o7gPRvtWIyIVYuoTzjJn93QDFpDqXDmJyIXhw dqgw== 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=ayy/Y43IWnCQTSLM6yaVvA70ZVnaeYZHB37BEMMueeg=; b=Q8U+kU8rBTr4NbaKjfKqMEVbWpe4RF1bAHJqPXNSixFyDuf0z5fA+kx7RKV5oNKSf7 EqJtp0aT8qJQZYuVSyhFvywYYAjLA9Pq+BjeYUd4vlXxb1eyX6jFpxYEN8i2QB2ue7Xw sKpGigieIzecF4rNw8K8ysRPMvaHiJjECnWEVf2B/lVyz1Q99/DKX8RjG4KZEDUULUWJ kbJELZPO7yXwgfQ+1PrlZNlR80x+FE2W5xeTLrz1TVAVcZvDpsUJ3v1PXnFkG8MVZgGv FwV5WlzAomQWnum3fzVX14yQmbCXC5oDszgyd11Btxe5353GrMfXZv1cf5y0lyC7S4Kk /p3Q== X-Gm-Message-State: AOAM533e55kZtUDt2YSF/BUhyq+qkEy6/jP9QH3/kuyaRYQV1kjwuxCE mY03o4i0Qlun9Um9OQ+DVwwdGEO3vjs= X-Google-Smtp-Source: ABdhPJx/OxEVsvNea9PUdkE0101FTRaZdkrM6bQNjL4cy161FapDUL9noukleiHg8JtWCSqC6hk5SA== X-Received: by 2002:adf:eb4d:: with SMTP id u13mr4511463wrn.146.1604081211962; Fri, 30 Oct 2020 11:06:51 -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 g66sm5669478wmg.37.2020.10.30.11.06.51 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 30 Oct 2020 11:06:51 -0700 (PDT) To: internals@lists.php.net References: <446c9894-191f-e53b-4534-31c4e7b91d7b@gmail.com> <881e2335-5f81-0d79-6a17-06bc20f14223@gmx.net> <873361ff-02c8-b9df-9b7a-c9e89e25a880@gmx.net> <7F014A40-2D47-4DA6-8A38-59518218F9CA@stitcher.io> <9A58B811-37C4-46C8-A7A9-B558752362F3@stitcher.io> Message-ID: <9aa03cfc-a067-cc07-8dd9-50b95ae681ed@gmail.com> Date: Fri, 30 Oct 2020 18:06:51 +0000 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.12.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-GB Subject: Re: [PHP-DEV] RFC: Support for multi-line arrow functions From: rowan.collins@gmail.com (Rowan Tommins) On 29/10/2020 22:49, Benjamin Morel wrote: > On Tue, 6 Oct 2020 at 14:09, G. P. B. wrote: > > Actually there is a precedent in PHP where braces don't create a new > scope: > $a = 1; > > { > $a++; > } > > echo $a; // 2 This is true. Indeed, in general, scope in PHP is not to the nearest pair of braces - if statements and loops don't create a new scope - but it is, with very few exceptions, to the current function, with no automatic inheritance/capture from the outer scope. The fact that arrow functions *don't* have braces makes them look less like a normal function, so it is less surprising that they capture from their surrounding scope. The fact that they are limited to a single expression also reinforces that they are intended for a specific use case, and are not intended as a replacement for all anonymous functions. > Let's put it into context, here is how one of my service methods looks like: > > return $connection->transactional(function() use ($authorId, > $activityId, $parentId, $comment) { > > ... and this is not the largest one. Let's see how it would look like with > this proposal: > > return $connection->transactional(fn() => { Just to be clear, the major gain here is not replacing the 10 characters "function()" with the 7 characters "fn() =>", it is eliminating the list of captured variables. So you would get equally clean code with a "capture all" syntax, such as: return $connection->transactional(function() use(*) { or Sara's suggestion from earlier today: return $connection->transactional(function() use($) { If I've counted right, this takes your example from 46 characters down to 17; but more importantly, that 17 is fixed no matter how many extra variables you want to capture or how long you make their names. > And I'm not worried about scope leaking: > usually, the closure takes up most of my service method. To me, this illustrates exactly why we should have an explicit "auto-capture" syntax, rather than extending arrow functions: this is a form you would use in specific situations where you know it's appropriate, *not* a general-purpose replacement for the existing syntax. As well as being shorter than the current syntax, arrow functions would remind people of JavaScript, where it's considered best practice to use arrow functions as much as possible, because they have *better* scoping - in particular, they automatically bind "this", which PHP closures already do. There are certainly use cases where automatic capture leads to more elegant code than explicit capture; but there are also plenty of cases where it makes code hard to follow, and leads to subtle bugs. That's why I would like the two syntaxes to feel like equal options to choose between. Regards, -- Rowan Tommins (né Collins) [IMSoP]