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

4

u/redditthinks Jul 26 '13 edited Jul 26 '13

Another thing, implicit parentheses should be less greedy. For example:

a = myFunc x

Now I realize I want to add a number to it:

a = myFunc x + 1

This looks very ambiguous, and CoffeeScript compiles it to myFunc(x+1) instead of myFunc(x) + 1.

Something similar to this happened to me and led to a subtle bug. This is particularly an issue when you want a default value:

elem.getAttribute 'data-info' or ''

I think it should stop capturing when it meets an operator.

Also, better multiline strings (ala Python):

message = ('Hello'
           ' world!')

This is not possible now in CoffeeScript:

message = "Hello
            world!"

will give:

message = "Hello                world!";

5

u/jashkenas Jul 26 '13

implicit parentheses should be less greedy.

I disagree. The rule that implicit parentheses (generally) follow, is that they extend to the end of the line, or to the end of the block that ends the line. To riff on your examples, it allows for uses like this:

print message or defaultMessage

bank.deposit currentBalance + incomingCheck

... and if you need tighter binding, you simply write the parens:

total = bank.lookup("checking") + bank.lookup("savings")

This is not possible now in CoffeeScript

Oh, but it is ;) If you don't mind the new line, block strings gobble-up all of the left-hand indentation for you: http://coffeescript.org/#strings

1

u/[deleted] Jul 26 '13

The thing I sometimes run into in CS is that defining a lambda as the first argument of a function when there are more than one argument does only work if you put the thing in brackets:

foo((->), bar)

1

u/jmhnilbog Aug 01 '13

What's wrong with:

foo (x)->
  console.log 'x'
, bar

I'm assuming your lambda would have some content. Without any, this won't work.

1

u/[deleted] Aug 01 '13

I didn't know that that works. Thanks.

1

u/redditthinks Jul 26 '13 edited Jul 26 '13

The rule that implicit parentheses (generally) follow, is that they extend to the end of the line, or to the end of the block that ends the line.

Hmm, good point. However, that encourages what I see as 'floating arguments'. Extending to the end of the line is a good idea if it doesn't meet an operator, like the example on the front page:

console.log sys.inspect object

Furthermore, I think it would be good to encourage wrapping a long argument with operators in parenthesis.

If you don't mind the new line

That's the problem :)

A common example is a long URL which are sensitive to new lines, extra spaces, etc. I think the current implementation of double quoted, multiline strings is very limited as it only works properly in the outermost scope, otherwise it doesn't respect indentation, making the code look out of place (and interfering with Vim's folding ;) ).

1

u/acdha Jul 26 '13

Is there a way to disable implicit parenthese entirely? I like a lot of what's in CoffeeScript but that class of behaviour has lead to bugs, usually subtle, in every language where I've encountered it.