Re: [PHP-DEV] [PATCH] [RFC] Closures and lambda functions in PHP
by Alexander Wagner other posts by this author
Jun 19 2008 5:01PM messages near this date
Re: [PHP-DEV] [PATCH] [RFC] Closures and lambda functions in PHP
|
Re: [PHP-DEV] [PATCH] [RFC] Closures and lambda functions in PHP
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
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
Thread:
Christian Seiler
Wez Furlong
Dmitry Stogov
Stanislav Malyshev
Alexander Wagner
Dmitry Stogov
Andi Gutmans
Christian Seiler
Lukas Kahwe Smith
Sebastian Bergmann
Marcus Boerger
Markus Fischer
Troels Knak-Nielsen
Christian Seiler
Dmitry Stogov
Larry Garfield
Christian Seiler
Dmitry Stogov
Christian Seiler
Lars Strojny
Stanislav Malyshev
Marcus Boerger
Lars Strojny
Troels Knak-Nielsen
Larry Garfield
Marcus Boerger
Dmitry Stogov
Andi Gutmans
Alexander Wagner
Andi Gutmans
Alexander Wagner
Alexander Wagner
Christian Seiler
Alexander Wagner
Lars Strojny
Dmitry Stogov
Marcus Boerger
Lars Strojny
Dmitry Stogov
Alexey Zakhlestin
Federico Lebron
Dmitry Stogov
Rodrigo Saboya
lenar
Larry Garfield
Stanislav Malyshev
Marcus Boerger
Alexander Wagner
Lars Strojny
Larry Garfield
Robert Cummings
Rodrigo Saboya
Alexander Wagner
Christian Seiler
Chris Stockton
Alexander Wagner
Troels Knak-Nielsen
Andi Gutmans
Marcus Boerger
Christian Seiler
Lukas Kahwe Smith
Gwynne Raskind
Andi Gutmans
Christian Seiler
Stanislav Malyshev
Kalle Sommer Nielsen
Troels Knak-Nielsen
Lars Strojny
Alexander Wagner
Stanislav Malyshev
Alexander Wagner
Andi Gutmans
Marcus Boerger
Kalle Sommer Nielsen
Troels Knak-Nielsen
Stanislav Malyshev
Alexey Zakhlestin
Chris Stockton
Alexey Zakhlestin
Gwynne Raskind
Stanislav Malyshev
Christian Seiler
Gwynne Raskind
Stanislav Malyshev
Richard Quadling
Christopher Jones
Marcus Boerger
Steph Fox
Christian Seiler
Marcus Boerger
Stanislav Malyshev
Lars Strojny
Christian Seiler
Stanislav Malyshev
Marcus Boerger
Marcus Boerger
Andrei Zmievski
Stanislav Malyshev
Stanislav Malyshev
Alexey Zakhlestin
Chris Stockton
Christian Seiler
Christian Seiler
Larry Garfield
Edward Z. Yang
Christian Seiler
Larry Garfield
Christian Seiler
Nathan Nobbe
Christian Seiler
Alexey Zakhlestin
Larry Garfield
Philip Olson
|