Just when I thought that this discussions was dying
2010-02-23 22:50, Stanislav Malyshev wrote:
Everybody on the internet knows that Wikipedia is the ultimate source of
knowledge, and it says:
In computer science, a closure is a first-class function with free
variables that are bound in the lexical environment. Such a function is
said to be "closed over" its free variables.
I am catchÃng the irony from the first sentence and will not use
Wikipedia as an authoritative source ;-)
It also says:
"The term closure is often mistakenly used to mean anonymous function.
This is probably because most languages implementing anonymous functions
allow them to form closures and programmers are usually introduced to
both concepts at the same time. These are, however, distinct concepts."
http://en.wikipedia.org/wiki/Closure_%28computer_science%29
This sentence was addede to the Wikipedia article 2008-09-08
http://en.wikipedia.org/w/index.php?title=Closure_(computer_science)&oldid=237056383
That was more than 150 revisions of the article ago, and no other editor
has removed it = this is not a contested issue in that community.
Which is my point, the PHP community should not use the word closure i a
way that is not aligned with how it is used otherwise.
Let's move on to some more definitive sources:
See also JavaScript, The Definitive Guide, by David Flanagan, (5th ed)
section 8.8.4 on page 143f:
"This *combination of code and scope* is known as a closure in the
computer science literature" (p.144 emphasis added)
Martin Fowler:
"So the first crucial point about closures is that they are a block of
code plus the bindings to the environment they came from."
http://martinfowler.com/bliki/Closure.html
And back to JavaScript:
This is a typical quote from the ECMAScript 5th edition standard:
"Let closure be the result of creating a new Function object as
specified in 13.2 with an empty parameter list and body specified by
FunctionBody. Pass in the LexicalEnvironment of the running execution
context as the Scope."
From section 11.1.5 on page 66.
http://www.ecma-international.org/publications/standards/Ecma-262.htm
I think this describes what PHP is doing.
However, in the PHP manual as well as on this list, you seem to be
saying "closure" === "anonymous function" (which is what I'd rather see
called lambda).
This is not entirely correct, you are right. There's a difference
between anonymous function and closure, though in practice in PHP
anonymous functions are closures (though some of them are rather trivial
ones with no variables to "close over") and that's now the only way to
do closure in PHP (i.e. you can't have non-anonymous closure function).
Agreed that PHP does not have non-anonymous closure functions. My remark
that closures can be used with named functions was a reflection upon the
concept as a whole, not on PHP's implementation.
Also agreed PHP has anonymous non-closure functions as well, I hope.
This is what I am trying to address. Without the keyword "use" it is not
a closure, as Mathieu has already pointed out.
FWIW, I moved the discussion to the PHP Doc list, as the discussion here
seemed to die out and fixing the manual would be the most obvious first
step.
--
Keryx Web (Lars Gunther)
http://keryx.se/
http://twitter.com/itpastorn/
http://itpastorn.blogspot.com/
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php