Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:98199 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 32666 invoked from network); 4 Feb 2017 21:08:16 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 4 Feb 2017 21:08:16 -0000 Authentication-Results: pb1.pair.com header.from=me@kelunik.com; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=me@kelunik.com; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain kelunik.com from 81.169.146.162 cause and error) X-PHP-List-Original-Sender: me@kelunik.com X-Host-Fingerprint: 81.169.146.162 mo4-p00-ob.smtp.rzone.de Received: from [81.169.146.162] ([81.169.146.162:28960] helo=mo4-p00-ob.smtp.rzone.de) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 7C/3A-38491-DB246985 for ; Sat, 04 Feb 2017 16:08:15 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; t=1486242491; l=11552; s=domk; d=kelunik.com; h=Content-Type:Cc:To:Subject:Date:From:References:In-Reply-To: MIME-Version; bh=WJmB1HhYR+VkaQB8vYDGeEUH5Z8tX3wGOKXEZQl0KWU=; b=HryoolQRpUS8+/KeUbyZ8qief9JDc2C0ewJ7u0UXaChZWMMU55cwdphXzLPMKBzY19 A4t6AMqFZahBUQBHlgaXPBM+VEPrEzeD+xubVvcYE6jaO+HoD3HdMzMSiHyvOJWkjAl4 ObhAwwod1XuCq2+5I7FMHVpnUm+8Z4dNXZ68c= X-RZG-AUTH: :IWkkfkWkbvHsXQGmRYmUo9mls2vWuiu+7SLDup6E67mzuoNJBqD/t9k= X-RZG-CLASS-ID: mo00 Received: from mail-qt0-f177.google.com ([209.85.216.177]) by smtp.strato.de (RZmta 39.12 AUTH) with ESMTPSA id Y04c1ct14L8Bpde (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (curve secp384r1 with 384 ECDH bits, eq. 7680 bits RSA)) (Client did not present a certificate) for ; Sat, 4 Feb 2017 22:08:11 +0100 (CET) Received: by mail-qt0-f177.google.com with SMTP id x49so75871473qtc.2 for ; Sat, 04 Feb 2017 13:08:10 -0800 (PST) X-Gm-Message-State: AMke39nnLLmA0O16SYIh1ctw2EdpdD7RReWaXtU3KGEzpZW8heQRNr6CPDigW55kh7P7I+cXmt5m3YgEielYDw== X-Received: by 10.237.37.50 with SMTP id v47mr2994301qtc.126.1486242490335; Sat, 04 Feb 2017 13:08:10 -0800 (PST) MIME-Version: 1.0 Received: by 10.12.144.132 with HTTP; Sat, 4 Feb 2017 13:08:09 -0800 (PST) In-Reply-To: References: Date: Sat, 4 Feb 2017 22:08:09 +0100 X-Gmail-Original-Message-ID: Message-ID: To: Larry Garfield Cc: PHP Internals Content-Type: multipart/alternative; boundary=001a114030a4afd3050547bacb32 Subject: Re: [PHP-DEV] Re: [RFC][Discuss] Arrow Functions From: me@kelunik.com (Niklas Keller) --001a114030a4afd3050547bacb32 Content-Type: text/plain; charset=UTF-8 2017-02-04 21:49 GMT+01:00 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. > I'd say the capture semantics aren't the first thing to be concerned with. First you want to know it's a anonymous function with one parameter, exactly what "function ($a)" tells you. For normal closures, "use ($n)" tells you it imports $n from the parent scope into the body enclosed in braces. For short closures, you see the "=>" and an expression and know that any outer variable you use will be imported from the parent scope. Regards, Niklas > 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 > > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > --001a114030a4afd3050547bacb32--