Newsgroups: php.internals
Path: news.php.net
Xref: news.php.net php.internals:38343
Return-Path: <gwynne@wanderingknights.org>
Mailing-List: contact internals-help@lists.php.net; run by ezmlm
Delivered-To: mailing list internals@lists.php.net
Received: (qmail 44846 invoked from network); 18 Jun 2008 08:02:56 -0000
Received: from unknown (HELO lists.php.net) (127.0.0.1)
  by localhost with SMTP; 18 Jun 2008 08:02:56 -0000
Authentication-Results: pb1.pair.com header.from=gwynne@wanderingknights.org; sender-id=unknown
Authentication-Results: pb1.pair.com smtp.mail=gwynne@wanderingknights.org; spf=permerror; sender-id=unknown
Received-SPF: error (pb1.pair.com: domain wanderingknights.org from 208.97.132.83 cause and error)
X-PHP-List-Original-Sender: gwynne@wanderingknights.org
X-Host-Fingerprint: 208.97.132.83 sd-green-bigip-83.dreamhost.com Linux 2.4/2.6
Received: from [208.97.132.83] ([208.97.132.83:36491] helo=postalmail-a6.g.dreamhost.com)
	by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP
	id C9/E5-03518-E21C8584 for <internals@lists.php.net>; Wed, 18 Jun 2008 04:02:55 -0400
Received: from [192.168.2.192] (c-24-128-82-179.hsd1.ma.comcast.net [24.128.82.179])
	by postalmail-a6.g.dreamhost.com (Postfix) with ESMTP id B59ED88F8B
	for <internals@lists.php.net>; Wed, 18 Jun 2008 01:02:51 -0700 (PDT)
Message-ID: <FB990539-B2F4-4C78-AD84-FB79651660C9@wanderingknights.org>
To: PHP Developers Mailing List <internals@lists.php.net>
In-Reply-To: <cd5680a20806172336y4127faf4t37d409396482da34@mail.gmail.com>
Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes
Content-Transfer-Encoding: 7bit
Mime-Version: 1.0 (Apple Message framework v924)
Date: Wed, 18 Jun 2008 04:02:43 -0400
References: <4856A547.3080801@gmx.net> <698DE66518E7CA45812BD18E807866CE01B11811@us-ex1.zend.net> <cd5680a20806172336y4127faf4t37d409396482da34@mail.gmail.com>
X-Mailer: Apple Mail (2.924)
Subject: Re: [PHP-DEV] [PATCH] [RFC] Closures and lambda functions in PHP
From: gwynne@wanderingknights.org (Gwynne Raskind)

On Jun 18, 2008, at 2:36 AM, Alexey Zakhlestin wrote:
>> 1) I am not sure that the current semantics of the "lexical"  
>> keyword is great in all cases. Is the reason why you don't allow by- 
>> value binding so that we don't have to manage more than one lambda  
>> instance per declaration?
> by-reference binding is much closer to other languages symantics. I
> guess, that was the main reason Christian chose it.
> "by-value" may still exist, if people find, that they need it, but
> only in addition, please.
>
> lambda has to reflect changing state of context, to be truly useful

In Lua, the language in which I've seen the most of closures and  
lambda, lexical scoping is handled this way:

someVariable1 = "asdf";
someVariable2 = "jkl;";
SomeFunction = function()
	local someVariable2 = "1234";
	
	print someVariable1.." "..someVariable2.."\n";
end
print gettype(SomeFunction).."\n";
SomeFunction();
someVariable1 = "qwer";
someVariable2 "0987";
SomeFunction();

The resulting output of this code fragment would be:
function
asdf 1234
qwer 1234

The Lua interpreter handles this by resolving variable references as  
they're made; "someVariable1" is looked up in the closure's scope and  
not found, so the interpreter steps out one scope and looks for it  
there, repeat as necessary. Once found outside the closure's scope,  
something similar to the proposed "lexical" keyword happens. Closures  
and lexical variables can be nested this way, to the point where a  
single variable in a sixth-level closure could still have been  
originally found in the global scope.

I'm not sure this would work for PHP, I'm curious what others think.

Of course, that fragment does a very poor job of showing off the  
extreme flexibility of Lua with regards to functions and scoping, but  
hopefully it illustrates the concept.

-- Gwynne, Daughter of the Code
"This whole world is an asylum for the incurable."