TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Immutability and Blocks, Lambdas and Closures

67 pointsby hanifviraniabout 14 years ago

4 comments

ekiruabout 14 years ago
The real issue is that Python's for construct doesn't create a new lexical scope for the body.<p>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') {...}").<p>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?).
评论 #2403288 未加载
stdbrouwabout 14 years ago
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 <i>is</i> a rather confusing phenomenon for newcomers.
评论 #2403298 未加载
glenjaminabout 14 years ago
Not having a closure around loop blocks is only confusing if you learnt a language that does have block scope in all cases.<p>For my part, I always found having block scope inside conditionals to feel weird.
adambyrtekabout 14 years ago
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.
评论 #2403598 未加载
评论 #2403516 未加载