Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:98196 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 27850 invoked from network); 4 Feb 2017 20:50:01 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 4 Feb 2017 20:50:01 -0000 Authentication-Results: pb1.pair.com header.from=larry@garfieldtech.com; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=larry@garfieldtech.com; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain garfieldtech.com from 66.111.4.25 cause and error) X-PHP-List-Original-Sender: larry@garfieldtech.com X-Host-Fingerprint: 66.111.4.25 out1-smtp.messagingengine.com Received: from [66.111.4.25] ([66.111.4.25:39730] helo=out1-smtp.messagingengine.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 22/49-38491-47E36985 for ; Sat, 04 Feb 2017 15:50:01 -0500 Received: from compute7.internal (compute7.nyi.internal [10.202.2.47]) by mailout.nyi.internal (Postfix) with ESMTP id 4A5C82067E for ; Sat, 4 Feb 2017 15:49:54 -0500 (EST) Received: from frontend2 ([10.202.2.161]) by compute7.internal (MEProxy); Sat, 04 Feb 2017 15:49:54 -0500 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-sender:x-me-sender:x-sasl-enc:x-sasl-enc; s= smtpout; bh=OiayDXY9WC1hStgP/Qk5U6O8Pwc=; b=ttJ/ds28XPKg1Spx7gpF vrDNVqGLGGmWs7FAWfiLidIgEUgPH5bOvC8ftyeEDjwpNmXQYGFW522LWv2CTLIQ frmCZQ3eZk+UgXNAVHPevYk1MvJaBtrX8g0+ThmOxkzpuC6Hk1hDVS5SDX4Ph77X h8Iab5cjt/moX4wvg57UwRw= X-ME-Sender: X-Sasl-enc: cM6IOVd7zvT29pLYoRaiQAoB81tV0u5TA9sw5BCto5Cj 1486241394 Received: from [192.168.42.5] (c-50-178-40-84.hsd1.il.comcast.net [50.178.40.84]) by mail.messagingengine.com (Postfix) with ESMTPA id 0DDB32414B for ; Sat, 4 Feb 2017 15:49:54 -0500 (EST) To: internals@lists.php.net References: Message-ID: Date: Sat, 4 Feb 2017 14:49:53 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; 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] Re: [RFC][Discuss] Arrow Functions From: larry@garfieldtech.com (Larry Garfield) On 02/03/2017 11:53 AM, Levi Morrison wrote: > >> Thanks to everyone who has participated in the discussion thus far. >> Primarily the feedback has been directed at the `fn` keyword. Let me >> provide two benefits and drawbacks of using `fn` as a keyword: >> >> 1. `fn` is searchable in search engines and in our manual >> 2. Is more intuitive than just syntax >> >> However, `fn` does have downsides: >> >> 1. Can break existing code >> 2. We already have a similar keyword `function` >> >> To that end, I'd like to gauge interest in a pure syntax based >> alternative that is similar to Rust and Ruby. >> Instead of: >> >> fn(params) => expr >> >> What about: >> >> |params| => expr >> >> This trades the advantages of the keyword for the advantages of pure >> syntax, and happens to be two characters shorter. To be explicit: >> >> 1. Preserves 100% backwards compatibility >> 2. Avoids having two keywords that both mean "function" >> 3. Is not easily searchable in engines or in the manual >> 4. Is a tad bit shorter >> >> What do you think, Internals? > One more thing: I'd like to re-emphasize that the syntax that > JavaScript uses and the one that HHVM/Hack uses are ambiguous in the > current class of our grammar. The following will not work unless we > move to a more powerful grammar and parser class: > > (params) => expr > (params) ==> expr > > This is why an un-ambiguous prefix is necessary: the prefix breaks the > ambiguities. The syntax I have suggested in the RFC and the one I > suggested just now are not ambiguous because distinct prefixes: > > fn(params) => expr > |params| => expr > > I look forward to more discussion! Off hand, either of the above options would be acceptable to me. The union type concern is a valid one, but as noted I don't know how much of an issue that would be in practice even if we ever did get union types. (Now, if we also added defined type aliases... :-) ) I am definitely not a fan of reusing `function`. 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. Additionally, to the point that was raised about developers being able to understand syntax reused in different context, that's true... if the context is in fact different. A bitwise AND and a reference are extremely different situations so there's no reason for my brain to expect both to mean the same thing. One is never a viable substitute for the other. In contrast, however, we're talking about two different syntaxes for defining anonymous functions that have different closure semantics. Reusing `function` would mean: $n = 2; $x = function ($a) use ($n) { return $a * $n; } $y = function ($a) => $a * $n; Are equivalent. But in order to know what the capture semantics are I can't tell from the start of the expression. I have to look halfway down the line to know what the behavior is going to be for these two extremely similar actions. In contrast: $n = 2; $x = function ($a) use ($n) { return $a * $n; } $y = fn($a) => $a * $n; $z = |$a| => $a * $n; I in versions $y and $z, I can tell right from the beginning of the line which variant of "anonymous function capture logic" I will be dealing with. I know from character 1 or 2, not character 12. And then there's the extra typing and visual space taken up, which is not something to dismiss entirely. Also, as someone else mentioned this syntax *seems* like it would support nesting. Compare the following: $x = function($a) => function($b) => function($c) => $a * $b * $c; $y = fn($a) => fn($b) => fn($c) => $a * $b * $c; $z = |$a| => |$b| => |$c| => $a * $b * $c; print $x(1)(2)(3); // prints 6 Which of these is more readable? Id' argue it's not $x. --Larry Garfield