Re: [PATCH] [RFC] Closures and lambda functions in PHP

From: Date: Fri, 20 Jun 2008 00:55:48 +0000
Subject: Re: [PATCH] [RFC] Closures and lambda functions in PHP
References: 1 2 3 4  Groups: php.internals 
Request: Send a blank email to internals+get-38440@lists.php.net to get a copy of this message
Hi!
First, a comment from haskell-land: http://www.haskell.org/pipermail/haskell-cafe/2008-June/044533.html http://www.haskell.org/pipermail/haskell-cafe/2008-June/thread.html#44379
Thanks for the links, very interesting. Even a couple of comments in the thread going beyond "PHP sucks" and really discussing the matter. :) Best account is this: * A closure must only keep alive the varables it references, not the whole pad on which they are allocated [Check] * A closure must be able to call itself recursively (via a higher-order function typically) [Check, since you can use variable you assigned closure to inside the closure, if I understand correctly] * Multiple references to the same body of code with different bindings must be able to exist at the same time [Check] * Closures must be nestable. [Dunno - does the patch allow nesting and foo(1)(2)?]
Getting the same behaviour out of PHP should not be as difficult as this:
Well, I don't see any other way if you use references. Variables _are_ mutable in PHP. You could, of course, use copies, but then you'd lose ability to update. Maybe if we drop "lexical" and use Dmitry's proposal of $arr[$i] = function () ($i) { return $i; }; where ($i) would be copy, (&$i) would be by-ref, then it'd be easier to control it. I know function()() is weird, but not everybody likes lexical either :) Maybe we can do lexical &$y, but that looks weird too...
Of course this would make lexical behave quite differently from global in this
I wouldn't spend too much thought on making lexical work like global. global is for different purpose (and with $GLOBALS is obsolete anyway :) -- Stanislav Malyshev, Zend Software Architect stas@zend.com http://www.zend.com/ (408)253-8829 MSN: stas@zend.com

« previous php.internals (#38440) next »