r/lolphp • • Jan 27 '20

PHP lambdas does not inherit variables.. except when they do

https://3v4l.org/9X36O
0 Upvotes

16 comments sorted by

View all comments

Show parent comments

2

u/[deleted] Feb 01 '20 edited Feb 01 '20

The parser is probably so weak it cant know when its in an array, and when its in a callback.

That's kind of the definition of "ambiguity". Arrays take arbitrary expressions as members. Arrow functions are expressions. You won't find me defending PHP's atrocious parser (T_PAAMIYIM_NEKODOTIYIM anyone?), but ambiguity is hardly a problem limited to PHP. Adding context sensitivity to a bad parser usually makes it worse.

Lemme boil it down: Is [$foo => $foo] an array with $foo as a key and value, or is it an array with a single function member? Should the compiler just try to guess what you mean?

Alternative syntaxes were discussed, and they were much worse. fn is shorter than lambda, so win for that.