Re: [RFC][Discuss] Arrow Functions
| From: | Sara Golemon | Date: | Tue, 30 May 2017 03:15:39 +0000 |
| Subject: | Re: [RFC][Discuss] Arrow Functions | ||
| References: | 1 2 3 4 5 6 7 8 9 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-99250@lists.php.net to get a copy of this message | ||
On Mon, May 29, 2017 at 10:48 PM, Levi Morrison <levim@php.net> wrote:
> Based on the discussion there are a few different syntax choices that
> we probably just need to vote between. It's a feature that people seem
> to want but everyone seems to prefer a different syntax choice.
>
> 1. fn(params) => expr
> 2. function(params) => expr
>
> 3. (params) ==> expr
> 4. (params) => expr
>
TL;DR - I'll be happy with whatever the group can agree on, with a
personal ordering of: 1 > 3 > 2.
I excluded #4 because I just don't see how you disambiguate this: [
($x) => $x ] . It's already valid syntax: https://3v4l.org/NedDu .
With zero or 2+ args it's disambiguable, but one arg has to be
possible or it's a no-go.
> This is why everyone is so divisive. All options have drawbacks.
> Additionally some people don't like binding by value and would prefer
> ref, and others really would be against by-ref.
>
Again, I'll accept the group's opinion because I don't care enough to
block it, but I'm registering my preference for the record, as by-ref.
> Which brings me to an option I don't think was ever discussed on list:
>
> [](params) => expr // binds no values
> [=](params) => expr // binds by value
> [&](params) => expr // binds by reference
>
As a C++ developer, I have no inherent problems with this **in
theory**, but I agree with your statement that it's a bit symbolic
soupish. I also suspect that we'll run into grammar ambiguities.
Imagine: [ []($x) => $x ]. This is current legal syntax (at the
point of compilation) because it *could* be interpreted as an array
with a single element (value $x) who's key is the result of calling a
function defined by an empty array passing a single arg ($x). It's
invalid at runtime (See: https://3v4l.org/IcK3B ), but that
means
special casing our way around the ambiguities.
Further, if we're going to follow the C++ model, we'd be well suited
to allow explicit per-var by-val/by-ref syntax, e.g.:
[$foo, $bar]() => $foo * $bar
Again, this is legal syntax to the parser, but now it's even
potentially current legal syntax to the runtime (assuming
is_callable([$foo, $bar]).
So yeah, probably doesn't fit PHP, but good thinking outside the box. :D
-Sara