Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:38439 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 50008 invoked from network); 20 Jun 2008 00:00:41 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 20 Jun 2008 00:00:41 -0000 Received: from [127.0.0.1] ([127.0.0.1:27580]) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ECSTREAM id C9/31-57468-623FA584 for ; Thu, 19 Jun 2008 20:00:38 -0400 Authentication-Results: pb1.pair.com header.from=waqner@gmx.net; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=waqner@gmx.net; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmx.net designates 213.165.64.20 as permitted sender) X-PHP-List-Original-Sender: waqner@gmx.net X-Host-Fingerprint: 213.165.64.20 mail.gmx.net Linux 2.6 Received: from [213.165.64.20] ([213.165.64.20:44909] helo=mail.gmx.net) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 16/60-57468-4DFEA584 for ; Thu, 19 Jun 2008 19:46:29 -0400 Received: (qmail invoked by alias); 19 Jun 2008 23:46:25 -0000 Received: from p5087EECC.dip.t-dialin.net (EHLO p5087eecc.dip.t-dialin.net) [80.135.238.204] by mail.gmx.net (mp046) with SMTP; 20 Jun 2008 01:46:25 +0200 X-Authenticated: #27239598 X-Provags-ID: V01U2FsdGVkX196uPYGpDJ6MLFQhYh813oNhYxY5suLnXgT0bnsxU SoexQYRIQn07h7 To: internals@lists.php.net Date: Fri, 20 Jun 2008 01:46:25 +0200 User-Agent: KMail/1.9.9 Cc: Christian Seiler References: <4856A547.3080801@gmx.net> <698DE66518E7CA45812BD18E807866CE01B70D73@us-ex1.zend.net> <48596C9E.2080605@gmx.net> In-Reply-To: <48596C9E.2080605@gmx.net> MIME-Version: 1.0 Content-Disposition: inline Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-ID: <200806200146.25898.waqner@gmx.net> X-Y-GMX-Trusted: 0 Subject: Re: [PHP-DEV] [PATCH] [RFC] Closures and lambda functions in PHP From: waqner@gmx.net (Alexander Wagner) 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 On Wednesday 18 June 2008, Christian Seiler wrote: > Frankly, I don't really see a problem with using references. It fits > into what's already there in PHP and it assures that closures have the > necessary properties to make them useful. References are necessary, but an easy way to obtain copies of variables from the lexical context would be really nice. I have been introduced to functional programming through Haskell, where values are immutable, so a reference is basically the same as a copy. I like this behaviour because it makes closures distinctly non-dangerous by default. Getting the same behaviour out of PHP should not be as difficult as this: for ($i = 0; $i < 10; $i++) { $loopIndex = $i; $arr[$i] = function () { lexical $loopIndex; return $loopIndex; }; unset ($loopIndex); } This is not only quite a hassle (making beer much cheaper than water, so to speak), I also believe it to be error-prone. A lot of programmers are going to forget that unset(). I would prefer something like this: for ($i = 0; $i < 10; $i++) { $arr[$i] = function () { lexical_copy $i; return $i; }; } An alternative would be to let lexical behavie like function parameters: - copies by default lexical $x; - objects referenced by default lexical $obj; - other references optional lexical &$y; Of course this would make lexical behave quite differently from global in this regard, decreasing consistency, but f*ck global, nobody should use that anyway. Better to have nice lexical closures. Gesundheit Wag -- Be careful about reading health books. You may die of a misprint. - Mark Twain