Re: [PHP-DEV] [RFC] Arrow functions / short closures

This is only part of a thread. view whole thread
  104737
March 15, 2019 10:02 nikita.ppv@gmail.com (Nikita Popov)
On Thu, Mar 14, 2019 at 3:04 PM Mathieu Rochette <mathieu@rochette.cc>
wrote:

> > Hi, > > it's nice to see this going on again :) > > while reading the rfc I was wondering, why do we need the "static" > keyword, couldn't static function be detected automatically ? >
I've added a note regarding this in https://wiki.php.net/rfc/arrow_functions_v2#this_binding_and_static_arrow_functions. The problem is that in PHP we cannot actually reliably detect whether or not a closure uses $this. Sure -- we can see literal $this uses, but some of them are implicit: fn() => Foo::bar() can make use of $this if $this is scope-compatible with Foo. fn() => call_user_func('Foo::bar') can as well. fn() => $a($b) can as well, if it so happens that $a = 'call_user_func' and $b = 'Foo::bar'. The result is that we cannot reliably detect whether $this is used or not -- we can only make a conservative analysis that may sometimes bind $this even though it's not needed. In which case I would prefer going with the more predictable behavior of always binding $this, and using "static" in the rare cases where someone really cares about this.
> I guess this apply to the existing closure syntax as well so to get more > on this topic I'll share my preferences on the syntax: I like the ==> or ~> > version because it also allow to drop the parenthesis when there is only > one argument and it's closer to what I'm used to in javascript > > I wouldn't mind having the rust syntax too but yeah, it would feel a bit > odd in PHP > > > thank you for your work on this ! > > Nikita Popov – Wed, 13. March 2019 16:57 > > Hi internals, > > > > Motivated by the recent list comprehensions RFC, I think it's time we > took > > another look at short closures: > > > > wiki.php.net/rfc/arrow_functions_v2 > > > > This is based on a previous (withdrawn) proposal by Levi & Bob. It uses > the > > syntax > > > > fn($x) => $x * $multiplier > > > > and implicit by-value variable binding. This example is roughly > equivalent > > to: > > > > function($x) use($multiplier) { return $x * $multiplier; } > > > > The RFC contains a detailed discussion of syntax choices and binding > modes. > > > > Regards, > > Nikita >