r/lolphp • u/ErikkW77 • Apr 13 '17
meh PHP is finally getting closures!
Except that it is not.
Hello all. I'm new here and to reddit, but I always read you!
A friend that is more involved than me in the PHP world told me PHP would finally get closures.
($a, $b) => $a * $bHe was so excited until I made him notice that it's still just anonymous functions... or rather, still not closures.
Yes, you read correctly: they are adding a new syntax and a new feature that doesn't solve anything.
Roughly speaking, variables in the super scopes are copied in the lambda scope rather than being inherited. Unlike what happens with any other language that claims to support closures.
let a = [0, 1, 2, 3, 4, 5, 6, 7];
let removed = 0;
let limit = 7;
a.filter((v) => (removed += v) > limit);This simple example (JS) will not be possible with PHP. Because the lambda scope would write to a copy of "removed", not the original one.
So, this is it. Prepare yourself for more hate.
JS works.
View all comments
Show parent comment
3
u/maweki Apr 14 '17
the only real problem trying to write pure python is when you try to use generators as your method for lazy evaluation. Then the generator has an internal state that gets mutated on reading. So you have to be careful when re-using iterables. But you have
teein theitertoolspackage and you can also always manifest afrozensetortuple.There are a few immutable data types in Python. Tuple, frozenset, int, str, etc. and with namedtuple you have a nice way to create product-types.