Re: [RFC][Discuss] Arrow Functions

From: Date: Tue, 31 Jan 2017 21:18:45 +0000
Subject: Re: [RFC][Discuss] Arrow Functions
References: 1 2 3 4 5  Groups: php.internals 
Request: Send a blank email to internals+get-98094@lists.php.net to get a copy of this message
> Am 31.01.2017 um 21:45 schrieb Andrea Faulds <ajf@ajf.me>: > > Hi Marc, > > Marc Bennewitz wrote: >> - Also I like the "use" keyword you have to define your variables >> >> -> Would it be helpful to allow "function () use (*) {}" to inline >> all available variables? > > I did think of that, but it's not as concise as not having to specify > use at all. You could take the opposite approach and auto-capture by > default, but permit use () to capture nothing, alongside regular > use (...) for explicit capture of variables. > > Thanks. > > -- > Andrea Faulds > https://ajf.me/ Hey Andrea, you realize that you are actually proposing quite a heavy BC break? Any code which uses the same variable than in parent scope and does not overwrite it before its first usage will break. E.g.: $foo = [1,2,3]; // more code working on foo doSomeAction(function($bar) { // waaah, $foo is now [1,2,3] instead of null foreach ($bar as $x) { $foo[] = $x; } return $foo; }); Or what I have seen in real code: $response_text = "Do action"; store_response($response_text); // much later doSomeAction(function() { $response_text .= "Do other action"; // Note that accidental .= instead of = store_response($response_text); }); The code still works as expected, because things are NULL-initialized currently. Now with implicit auto-capture this is breaking. No ahead of time errors like parse or compile errors, which warn you. Just subtly breaking. Depending on where it's done, it may not be even noticed soon (variables with the same name at different places tend to have compatible types), just misbehave subtly. This is not acceptable. Now, when you suggest "function($bar) use (*) => $foo + $bar", I feel like half the point of the RFC, the conciseness of the syntax, is missed. And I'd also reject function($bar) => $foo + $bar as this would mean the "function" keyword introducing different semantics depending on what follows. [apart from being still not as concise as "fn"] Thanks, Bob

« previous php.internals (#98094) next »