Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:98209 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 4966 invoked from network); 5 Feb 2017 17:51:56 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 5 Feb 2017 17:51:56 -0000 Authentication-Results: pb1.pair.com header.from=rowan.collins@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=rowan.collins@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.65 as permitted sender) X-PHP-List-Original-Sender: rowan.collins@gmail.com X-Host-Fingerprint: 74.125.82.65 mail-wm0-f65.google.com Received: from [74.125.82.65] ([74.125.82.65:34408] helo=mail-wm0-f65.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 03/C0-30708-C3667985 for ; Sun, 05 Feb 2017 12:51:56 -0500 Received: by mail-wm0-f65.google.com with SMTP id c85so17114149wmi.1 for ; Sun, 05 Feb 2017 09:51:56 -0800 (PST) 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; bh=+sx5bBWQ8kUnl6JtrmUXJ/cxwkcE6mHOavROJGSxFDo=; b=N138W1k16opbNPpLR6QjxKfszIWga3B2u31xg7TRi2zItf5AWgbDM0QuW8RhcCNEDc UbJ6YVTkDgFlozspX0rCQy7m3rndL152DQbAQb/7s9cwh1tl+QOpx3YeyWAiyCXxfPD+ Y9R+MEB6Tsfr1VD73HJDPl6chFADRQjU/jNzA8gDYOmB7MRKDq4VSN4saZlarDAWYqgy 0cEdkdUR9cMdwaP06kxwHcCTbi6RrBoEZiIcjwJ3lnwsc+/Fr+GE/2Ma47Xxk5GvNMOY DcOEFvnEnZUnbQemYvm3jUmwSobeidVS3wVHSbNebfwqR+uGfYRCNXZ8ZSlCbPU1aQcS h7eA== 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; bh=+sx5bBWQ8kUnl6JtrmUXJ/cxwkcE6mHOavROJGSxFDo=; b=KccxEmxowaOx76Z5UWvd05DRD4qgzfG5lf7zlN6arRkYBRtm+6cIyCcxYIPnj/XmRS YCQJ/VPwxUNZC4fH2y8cLIpCfFx+bk7lxHSMdnc6VwpDMkcmCwgUHGQ9ZUkmsoq1t5w2 Y0HSogYpqJdJ2eXMmtITf8ZEGltOGchY8V1nCJxSQMMOqI9PGsuRg+BXDuZz0lqKU4YK U8lOu2Shi0Y/dqUuxB6o0EFlPAVato0TOPaMW2Ctxd8hDTDFgfKqZ47++4LDCWfk7gds FDNXqEpYnlf0zUezRw6ehOgnOo80qS9kJPOBc7/g6rYAVmLEGeWGwudWp8vdOP3cCH7A 9Wmw== X-Gm-Message-State: AMke39kZeZLOG+V53HDZwR2xGfw6E5vZ3dDCq7x1g91Z5MO/lPsPyJmRr+DDLLRHOzL63A== X-Received: by 10.28.1.213 with SMTP id 204mr5264997wmb.70.1486317112980; Sun, 05 Feb 2017 09:51:52 -0800 (PST) Received: from ?IPv6:2a00:23c4:4bd2:6e00:510c:4fa6:b462:898f? ([2a00:23c4:4bd2:6e00:510c:4fa6:b462:898f]) by smtp.googlemail.com with ESMTPSA id w99sm56134193wrb.5.2017.02.05.09.51.51 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 05 Feb 2017 09:51:52 -0800 (PST) To: internals@lists.php.net References: Message-ID: <97bb5051-6413-b78a-e981-ad8858caec4c@gmail.com> Date: Sun, 5 Feb 2017 17:51:49 +0000 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.7.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] [RFC][Discuss] Arrow Functions From: rowan.collins@gmail.com (Rowan Collins) On 30/01/2017 17:55, Levi Morrison wrote: > Here is an example of an existing closure: > > function ($x) use ($arr) { > return $arr[$x]; > } > > This RFC proposes syntax and semantics to simplify this common usage to: > > fn($x) => $arr[$x] I think a lot of the disagreements and confusions on this thread come down to there being three fundamental views of what this new syntax is for: (a) an improved syntax for declaring closures (in the way that [$foo, $bar] is a shorter syntax for array($foo, $bar)) (b) improved *semantics* for declaring closures, where you don't have to list the variables to be captured (c) a new kind of closure designed for specific use cases, with its own syntax and semantics These aren't mutually exclusive aims, but which of them you have in mind makes a big difference to what you see as "better" or "worse". Many of those starting from view (a) or (b) would like to see the syntax extended to closures with full bodies, and perhaps eventually become the most common way of declaring closures. Even if not included initially, they want to design the feature with that in mind. From view (c), the syntax is expected to exist alongside existing closures forever, just as we have both "for" and "while" loops. For the record, I generally come from view (c). On 05/02/2017 11:10, Rasmus Schultz wrote: > as long as we're introducing a new form and syntax, why make it*less* capable than > ordinary PHP closures? This is a good summary of the natural position from view (a) or (b) - if this is going to be the new way of declaring closures, we should make it the best way of declaring closures. If it was just about making the syntax shorter, this would be fine - like [...] for array(...) - but the Big Idea here is automatic capture of variables, and that is a lot more than just a nicety of syntax. As Rasmus L. has pointed out, making automatic variable capture the norm would be a huge change in how PHP defined variable scope. Since the comparison to JS keeps coming up, consider this: in JS, *all* functions capture scope greedily when they're declared; in PHP, *no* functions capture scope automatically; the use() clause maintains that rule for anonymous functions, and (in my opinion) that is a feature, not a mistake that needs fixing. > At the very least, I feel there should be feature > parity - otherwise, code in the wild is going to continue to be a strange > mess of one form or the other, which doesn't help with language > comprehension at all To be clear, from view (c), there is absolutely nothing "strange" or "messy" about future code having both closures and arrow-functions; they are different tools, to be used in different circumstances. On 04/02/2017 07:40, Stephen Reay wrote: > So, is there any argument besides "6 more characters" for*not* using the function keyword? This question presumably comes from view (b) - if it's just another way of declaring a function, why do we need a new keyword? From view (a), those 6 more characters are enough argument in themselves, because if the aim is to make it shorter, why stop early? From view (c), the fact that the keywords are different is a feature not a bug - these aren't functions in the normal sense, they are a new way of defining a callable, so they *should* look different. On 04/02/2017 20:49, Larry Garfield wrote: > The way I see it, the point of a short-closure is to take a simple > expression and wrap it into a function so that it can be plugged into > a function context. I don't even think of it in the same way I would > a named function/method, more casting an expression to a function. This, meanwhile, is a good summary of view (c), and expresses my view better than I could myself. If anything, I agree that "fn" is *too much* like "function"; perhaps we should find something *more* distinct, like "lambda" ($foo = lambda($x) => $x * 2). That would have the advantage of giving a proper name to these new callables, and make them easier to discuss: "when your callback is too complex for a lambda, use a closure". Obviously that would imply permanently cutting off all chance of full function bodies with auto capture. Personally, I think that's a good thing, but I realise not everyone agrees. In short, remember that something that is "obviously better" for you may be "obviously worse" for someone else, simply because you are measuring against different aims. Regards, -- Rowan Collins [IMSoP]