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

Reply via email to