Re: [RFC][Discuss] Arrow Functions
| From: | Aaron Piotrowski | Date: | Sat, 04 Feb 2017 05:01:04 +0000 |
| Subject: | Re: [RFC][Discuss] Arrow Functions | ||
| References: | 1 2 3 4 5 6 7 8 9 10 11 12 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-98165@lists.php.net to get a copy of this message | ||
Hi Stephen,
> On Feb 3, 2017, at 10:38 PM, Stephen Reay <php-lists@koalephant.com> wrote:
>
>
>>
>> I absolutely agree that it's terrible… the point is that types (even potential
>> future union types) can be used with short closures, but it certainly doesn't mean they should
>> be. I don't think confusion with a non-existent and non-recommended way of coding should be a
>> reason against a particular syntax.
>
> Using type hints is a part of the language. It even has benefits that I can absolutely see
> being used here:
>
> array_map(function(Foo $x) => $x->bar());
>
> If Foo is a class/interface with a method of bar, your IDE can know that it's a valid
> method to call.
>
> That of course is in addition to the benefit of getting useful type errors from PHP itself.
You raise a good point about IDEs, types might have a place with short closures. Still, I don't
see union types, if they ever exist, being common here. Taking your example with the other proposed
syntax:
array_map(|Foo $x| => $x->bar());
Honestly I'm not sure which is better… personal preference at this point, each has their
merits.
>>
>>> As has been said before, PHP is relatively verbose compared to other languages. That
>>> makes it slightly more characters to type, but it also makes it absolutely clear what is happening.
>>
>> Using
function IMO makes it more likely to confuse a short
>> closure with a regular closure. Reusing the function keyword with
>> different semantics may result in some confusion.
>>
>
> So we should instead use syntax already used for bit wise OR, and further confuse the situation
> when the function has no parameters and reusing the logical OR operator. Brilliant.
This is certainly a downside, though expressions cannot start with a logical or bitwise OR, so that
will help differentiate them. However, it is hard to argue that function could be
confusing when something like || => $x ** 2 may be more confusing, so point taken.
Regardless, I still prefer the visual difference since the auto-capture behavior is so different
from other functions.
Cheers!
Aaron Piotrowski