Hi Stephen,
> On Feb 3, 2017, at 10:38 PM, Stephen Reay <[email protected]> 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
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php