On 24/03/2021 17:33, Christian Schneider wrote:
My guess would be that it was seen as one of PHP's big strength that variables 
don't just leak into other contexts but that it has to be done explicitly.

Now with arrow functions both the benefit of auto-capturing is bigger compared 
to the actual function 'body', it normally only spans a line or two and (for me 
personally) the absence of {} also kind of make the leakage fit my mental model 
more easily.

For these reasons I think I lean towards -1 for 
bothhttps://wiki.php.net/rfc/short-functions  
<https://wiki.php.net/rfc/short-functions>  
andhttps://wiki.php.net/rfc/auto-capture-closure  
<https://wiki.php.net/rfc/auto-capture-closure>  as they add more syntax without 
(IMHO) a huge benefit.

You might be able to convince me otherwise though ;-)


This pretty much sums up my view.

While making the syntaxes consistent is a valid aim, some focus seems to have been lost on the actual use cases being addressed.


With "function" spelled out in full, the "short functions" are shorter by exactly the length of the word "return", which is rather uninspiring. The examples exaggerate the benefit by sticking to PSR-12 formatting for the "long" version, when the language itself is perfectly happy for you to write this:

public function getFirstName(): string { return $this->firstName; }
public function getLastName(): string { return $this->lastName; }


As Christian says, automatic capture is a dramatic change to the language's scoping rules, and IMHO requires a more thorough justification on why the current syntax is burdensome. As I've said previously, my naive impression is that a long list of captured variables ought to be as bad a code smell as a long list of parameters; I'm willing to be proven wrong on this, but nobody has yet stepped forward with a real-life example.

If we do agree that we want to support auto-capture, I am also not convinced that "fn" is the right keyword to indicate it. I would be happier leaving that to mean "expression becoming a function", and extend the current "function ... use" syntax, e.g. "function($x) use (*)  { ... }". This also has the benefit of being extensible to supporting reference capture, e.g. "function($x) use (&$x, *)  { ... }" as short-hand for "function($x) use (&$x, $a, $b, $c, $d, $etc)  { ... }"


Regards,

--
Rowan Tommins
[IMSoP]

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php

Reply via email to