Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:38653 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 96581 invoked from network); 27 Jun 2008 02:58:13 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 27 Jun 2008 02:58:13 -0000 Authentication-Results: pb1.pair.com header.from=larry@garfieldtech.com; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=larry@garfieldtech.com; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain garfieldtech.com from 76.96.62.80 cause and error) X-PHP-List-Original-Sender: larry@garfieldtech.com X-Host-Fingerprint: 76.96.62.80 qmta08.westchester.pa.mail.comcast.net Received: from [76.96.62.80] ([76.96.62.80:46641] helo=QMTA08.westchester.pa.mail.comcast.net) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 61/02-20466-44754684 for ; Thu, 26 Jun 2008 22:58:12 -0400 Received: from OMTA13.westchester.pa.mail.comcast.net ([76.96.62.52]) by QMTA08.westchester.pa.mail.comcast.net with comcast id iamn1Z00E17dt5G5811d00; Fri, 27 Jun 2008 02:58:09 +0000 Received: from earth.ufp ([24.13.255.226]) by OMTA13.westchester.pa.mail.comcast.net with comcast id iqy41Z0064trKQ83Zqy4RZ; Fri, 27 Jun 2008 02:58:04 +0000 X-Authority-Analysis: v=1.0 c=1 a=IWnH0euKuzgA:10 a=K8k5FXa3V3kA:10 a=67BIL_jfAAAA:8 a=wTA4PRF6HdP2CNu2-nIA:9 a=S-bvm3IEiGaN0i6bd5EA:7 a=5wIQL04NJoJ1JRdN4wupyaMVe9kA:4 a=FHBbIDN7CdwA:10 a=LY0hPdMaydYA:10 Received: from localhost (localhost [127.0.0.1]) by earth.ufp (Postfix) with ESMTP id 27B54D8722 for ; Thu, 26 Jun 2008 21:58:04 -0500 (CDT) Received: from earth.ufp ([127.0.0.1]) by localhost (earth.ufp [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id NAS4xxtx6--A for ; Thu, 26 Jun 2008 21:58:04 -0500 (CDT) Received: from luna.local (unknown [192.168.42.104]) by earth.ufp (Postfix) with ESMTP id F3E5DD7C65 for ; Thu, 26 Jun 2008 21:58:03 -0500 (CDT) To: internals@lists.php.net Date: Thu, 26 Jun 2008 21:58:03 -0500 User-Agent: KMail/1.9.9 References: <4856A547.3080801@gmx.net> <485B908D.7000106@zend.com> <4863C299.1080002@gmx.net> In-Reply-To: <4863C299.1080002@gmx.net> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-ID: <200806262158.03526.larry@garfieldtech.com> Subject: Re: [PHP-DEV] [PATCH] [RFC] Closures and lambda functions in PHP From: larry@garfieldtech.com (Larry Garfield) On Thursday 26 June 2008 11:23:53 am Christian Seiler wrote: > Hi Dmitry, > > > I'm fine if you'll improve my patch (It's mainly yours :) > > I updated my closures RFC: http://wiki.php.net/rfc/closures > In my eyes, the following questions should be answered: > > * Do you want closures in PHP? > > I have not seen a single negative reaction to my proposal, so I > assume the answer to that is yes. ;-) Yea. :-) > * Which syntax should be used for lexical variables? Should references > or copies be created by default? > > This is far trickier. > > First of all: There must *always* be the _possiblity_ to create > references, else you can't call it closures. > > Second: I prefer the 'lexical' keyword, but I could live with the > use solution [function () use ($...)]. > > Third: There are several arguments against default referencing: > > * (use syntax) As they look similar to parameters and normal > parameters aren't passed by ref, this could be quite > odd. > * Loop index WTFs (see proposal) > * Speed? (You always read that refs are slower in PHP than normal > variable copies. Is that actually true?) > * While it is possible to simply add an & in front of the variable > name to switch to refs in "no refs default" mode, there is no > obvious syntax to use copies in "refs default" mode other than > unsetting the variable in the parent scope immediately after > closure creation. > > Fourth: There are several arguments for default referencing: > > * (lexical syntax) global also creates a reference, why shouldn't > lexical? > * Other languages *appear* to exhibit a very similar behaviour to > PHP if PHP created references. This is due to the fact that > other languages have a different concept of scope as PHP > does. > > Although the list of against arguments appears to be longer, I do > prefer using references by default nevertheless. But that's just > my personal opinion. I see these two issues as related, actually. Consider: $foo = function($a, &$b) { global $c; lexical $d; // ... } Since they look the same, you'd expect them to behave the same. However, global will import by reference and lexical by value. Hilarity ensues, and not the good kind. Naturally changing the behavior of global in this case is out of the question, and as you point out defaulting to reference and having an extra flag (a la &) to force it to value is unprecedented. I think most seem to agree that being able to pass by value or by reference at the developer's discretion is necessary. However, something in the function signature itself would naturally follow the behavior of function arguments: $foo = function($a, &$b) lexical ($d, &$e) { global $c; // ... } Here, $d and $e behave "as you'd expect them to", with the same visual parsing semantics as $a and $b. That to me is a much lower wtf factor than having global and lexical keywords that look alike but behave differently. I would therefore favor the signature-based syntax. As an aside, I did use "lexical" in the second example above deliberately. Personally I don't think re-using "use" here is wise, as that will make it seem namespace related when in fact it is not. I suspect the instances of function or constant names of "lexical" will be pretty minimal (although I admit to having no evidence to back up that suspicion.) (I would much much rather have a closures implementation that used "use" than not one at all, mind you; I will still jump for joy if it lands using "use", I just think it would be better using "lexical".) > * Are you OK with the change that $this is only stored when needed? Ignoring the compiler-level concerns, about which I know nothing useful, doesn't this introduce a bit of wtf? "All lexically-imported variables must be explicit or they don't exist... oh yeah, except for $this because it's special." To which the question is: If you are able to magically determine if $this is used and optimize accordingly, why can't you for anything else? To which the response is, I think: "Performance". That's still something of an inconsistency, however. Would it make the engine code any cleaner/messier if $this was required to be declared explicitly like everything else? > * Do you want closures in PHP 5.3? As a PHP developer I'd love to have closures in 2 years when I'm able to use PHP 5.3 instead of 5 years when I'm able to use PHP 5.4 or PHP 6. :-) I do understand the need to draw a line somewhere and justbloodyshipit(tm), however, so if that's the decision I can accept that. > Regards, > Christian You so rock. :-) -- Larry Garfield larry@garfieldtech.com