[Haskell-cafe] Lambda and closures in PHP -- could someone please comment?

Jules Bean jules at jellybean.co.uk
Thu Jun 19 02:25:05 EDT 2008


Richard A. O'Keefe wrote:
> The mutablity of r here really isn't a problem.  Nor is the mutability
> of variables _as such_ really the problem in the PHP proposal.
> The problem is that it's the *same* variable every time.  If PHP
> loops introduced new bindings on every iteration, this particular
> problem would not exist.

Well, arguably it's not only the loop variable that can be susceptible 
to this problem. There could be other variables in the loop body which 
change each time through (e.g. while loops). Consider this pseudo-code 
(sorry, my PHP is a bit rusty, this syntax is C really)

char c;

while (!eof(fp)) {
   c = getChar(fp);
   bind_event(... some lambda expression referencing c ...);
}

It's pretty surprising to the programmer if all that family of lambda 
expressions reference the *variable* c (and hence, in practice, its 
final value) rather than the *value* c.

Well, maybe that doesn't surprise everyone. It surprised me the first 
time I used closures in Javascript and judging by a few google searches 
I wasn't alone in that.

Jules


More information about the Haskell-Cafe mailing list