Re: [RFC][Discuss] Arrow Functions
| From: | Aaron Piotrowski | Date: | Sat, 04 Feb 2017 00:07:43 +0000 |
| Subject: | Re: [RFC][Discuss] Arrow Functions | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-98159@lists.php.net to get a copy of this message | ||
> On Feb 3, 2017, at 11:53 AM, Levi Morrison <levim@php.net> wrote:
>
> 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!
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php
> <http://www.php.net/unsub.php>
I personally prefer the
|params| => expr syntax.
I like the visual separation the syntax provides between auto-capturing, short closures and regular
closures. It gives the impression that it should behave differently, rather than just being a
shorter way of declaring a closure.
Below are a couple of real-world single line closures using the proposed syntax:
[1] Loop::defer(|| => ($this->when)($exception, $value));
[2] $this->watcher = Loop::delay($time, || => $this->resolve($value));
IMHO, the reduced boilerplate required makes it easier to discern what is happening.
Regards, Aaron Piotrowski
[1] https://github.com/amphp/amp/blob/master/lib/Coroutine.php#L45-L47
[2] https://github.com/amphp/amp/blob/master/lib/Pause.php#L21-L23