[Haskell-cafe] Lambda and closures in PHP -- could someone please
comment?
Luke Palmer
lrpalmer at gmail.com
Wed Jun 18 10:50:00 EDT 2008
On Tue, Jun 17, 2008 at 4:45 PM, Karoly Negyesi <karoly at negyesi.net> wrote:
> Hi,
>
> The PHP community is discussing the adding of closures and lambdas to
> the language, see the proposal at http://wiki.php.net/rfc/closures
>
> If someone with knowledge of both languages could take a quick look it
> would be great.
>
> Thanks a lot
>
> Karoly Negyesi
>
> Ps. I am not a member of the PHP internals team, I am just a PHP
> developer but I am very very interested in getting these in my
> favourite language.
Whew. Well I suspect you weren't expecting that kind of reaction. Or
maybe you were... I used to be a Perl developer, and it didn't take
long before I got a level 12 resistence to flame...
Anyway, the proposal looks mostly okay. I don't know that much PHP,
but I find the "lexical" keyword to be a nuisance. What are the
semantics if the lexical keyword is omitted? (i.e. does the
variable become function-local, global, what?) If it is consistent
with the rest of the language, it'll do.
There is a much more important point with closures: their
implementation cannot be half-assed! I'm not claiming that the patch
is--I have not reviewed it--but there is nothing worse than coming up
with a design that relies on a language feature you only later find
out has been nerfed in some way. Story of my life in C#. And nerfed
closures are especially bad, because it's so hard to predict the code
path.
What I mean by this is the following must all be supported:
* A closure must only keep alive the varables it references, not the
whole pad on which they are allocated (Python messed up here)
* A closure must be able to call itself recursively (via a
higher-order function typically) (Squeak messed up here IIRC)
* Multiple references to the same body of code with different bindings
must be able to exist at the same time (duh, that's kinda what makes
it a closure)
* Closures must be nestable.
Looking over the "Zend internal perspective" section, it looks like
that implementation will mostly work. There are a couple of red
flags, though:
* I would recommend only saving $this in the op_array structure if the
closure actually references $this -- if that is possible to deduce at
the time. Otherwise you might run into unexpected poor memory
performances in certain cases. (This kind of thing can make an
*asymptotic* difference in memory performance; i.e. bringing the
memory usage of an algorithm from O(1) to O(n), for example)
* I'm worried that nested closures do not work properly with this
implementation sketch. Here's a test case:
$f = function ($y) {
return function ($z) {
return $y + $z;
}
};
$f(1)(2) # should give 3
And congratulations, PHP, for adopting a most essential and powerful feature!
Luke
More information about the Haskell-Cafe
mailing list