Re: On closures and lamdba
| From: | Larry Garfield | Date: | Wed, 24 Feb 2010 05:47:28 +0000 |
| Subject: | Re: On closures and lamdba | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-47041@lists.php.net to get a copy of this message | ||
On Tuesday 23 February 2010 05:00:28 pm Ionut G. Stan wrote:
> > This is not entirely correct, you are right. There's a difference
> > between anonymous function and closure, though in practice in PHP
> > anonymous functions are closures (though some of them are rather trivial
> > ones with no variables to "close over") and that's now the only way to
> > do closure in PHP (i.e. you can't have non-anonymous closure function).
>
> Correct me if I'm wrong, but given the fact that PHP only* supports
> functions defined in the global space**, with the additional ability to
> import global variables using the global statement, wouldn't that make
> named functions able to close-over global variables?
>
> And, if the above is true, wouldn't it be consistent to support the use
> statement on named functions, and then deprecate the global statement?
>
> I remember one of the first implementation for closure, used a statement
> similar to global for closing over variables (the lexical statement).
IMO, globals could and should use a similar syntax to lexical closures:
function foo($a, &$b) global ($c, &$d) {
// ...
}
That would allow a parallel syntax, and allow for both by-value and by-
reference globals, which currently we cannot do. And closures/lambdas
could/should support the same syntax for globals.
--Larry Garfield