top | item 2402965

Immutability and Blocks, Lambdas and Closures

67 points| hanifvirani | 15 years ago |algorithm.com.au | reply

16 comments

order
[+] ekiru|15 years ago|reply
The real issue is that Python's for construct doesn't create a new lexical scope for the body.

In either Perl 5 or Perl 6, both of which use mutable variables, the corresponding code does the right thing (although Perl 5 only does so if you actually use a newly defined lexical: as in "for my $m ('do', 're', 'mi') {...}").

Unfortunately, Python's implicit declaration of lexicals means that it would be difficult to do a for loop with an iteration variable that isn't lexical to the loop body if that was the default (where would you put the "nonlocal" statement?).

[+] masklinn|15 years ago|reply
> The real issue is that Python's for construct doesn't create a new lexical scope for the body.

Yes and no, that's what leads the author to notice the "issue", but it does not actually solve the problem (if closing over a mutable environment is to be seen as a problem), it just pushes it back.

It can be argued that loops is the most common case where this is a problem (by quite a long shot) and working around that issue (by creating statement-level scopes or by using internal iterators [0]) would lead to the vast majority of users extremely encountering it. But still...

[0] internal iterators being the reason why Smalltalkers never encounter that issue, and rubyists and modern JS-developers (using Array.prototype.forEach or equivalent library-specific features) rarely do.

[+] stdbrouw|15 years ago|reply
This is why CoffeeScript introduced the "do" keyword. I'd have to agree that, while in Javascript in particular this is considered a rookie mistake, loop variables not being closed over is a rather confusing phenomenon for newcomers.
[+] sharat87|15 years ago|reply
CoffeScript is a very good language, but in my opinion, it should not be an excuse to not learn and understand such confusing behaviors of javascript. It will eventually bite any such newcomers. Not that you meant this, just saying.
[+] glenjamin|15 years ago|reply
Not having a closure around loop blocks is only confusing if you learnt a language that does have block scope in all cases.

For my part, I always found having block scope inside conditionals to feel weird.

[+] adambyrtek|15 years ago|reply
This problem was solved brilliantly by Clojure. Everything is immutable by default, and modifications of references happen at explicit points in the flow, with well-defined semantics.
[+] dons|15 years ago|reply
This problem was solved brilliantly by computer science in the 1970s. FTFY :-)
[+] Locke1689|15 years ago|reply
To be fair, I believe this behavior was just copied from Lisp, Scheme, Haskell, and ML.