r/programming • • Jul 25 '13

CoffeeScript's Scoping is Madness

http://donatstudios.com/CoffeeScript-Madness
207 Upvotes

315 comments sorted by

View all comments

Show parent comment

6

u/hderms Jul 25 '13

it doesn't even make sense to me why a for loop would return a collection. It seems very bizarre that it would have those semantics (it's 'for', not 'map')

5

u/[deleted] Jul 25 '13

Ruby works the same, every statement is also an expression. I never saw the use with for and each() either though, it just causes problems and confusion.

5

u/loz220 Jul 25 '13

I'm guessing because the cofeescript author(s) decided everything was going to be an expression even in situations where it's totally redundant or can bite you. Kind of like the example Plokryeran provided.

Now if you use coffeescript you always be aware of this behaviour. If you don't want your code to be doing needless work you have to end it with an empty return instead.

4

u/moohoohoh Jul 25 '13 edited Jul 25 '13

In Haxe everything is an expression, but for/while/try have types Void so 'behave' like statements.

If you do want to have this behaviour for a for loop, array comprehension gives it by encasing the for loop in [] eg [for (i in 0...10) i*10]

4

u/Plorkyeran Jul 25 '13

The short answer is that it's trying to be both map and for, and only partially succeeds at that.

Personally I'd prefer something a bit closer to Python's approach with list comprehensions. If you had to wrap the for block with parentheses to explicitly turn it into a map this problem would go away (and as it happens you already have to do so in most cases).