Hacker News new | ask | show | jobs
by dschooh 2399 days ago
> I wonder why they added the "fn()" thing on the left of the array functions.

There would be ambiguity without it: https://wiki.php.net/rfc/arrow_functions_v2#syntax

1 comments

But does it have to be =>? How about ~>?

    $a=[$x ~> $x*2, $x ~> $x*$x];
    $a[1](5); // 25
Looks cool to me.
~ is annoying to type on some keyboard layouts (Alt Gr and + onna German keyboard)

Also PHP tries to be "googlable". Typing "fn PHP" into Google should bring relevant hits, for people never seen such a construct.

> Also PHP tries to be "googlable".

If not insisting on the "google" part, there always is http://symbolhound.com/?q=php+%7E%3E

I noticed lately google is starting to understand symbols in certain contexts. Searching "php =>" actually does provide relevant results.
There's some rationale here[0]. Basically they'd need to use a parser that on ambiguities would have to go down all possible routes concurrently until all but one fail, which can lead to performance costs. Using a prefix keeps things simple.

[0] https://wiki.php.net/rfc/arrow_functions_v2#x_x_y1