Hacker Timesnew | past | comments | ask | show | jobs | submitlogin

    > If your function takes callbacks as the first parameters, 
    > and another value as the second (such as setTimeout), 
    > you end up with some really awkward syntax.
Hopefully no more awkward than the equivalent call in JavaScript.

    setTimeout (-> alert "later"), 300
Versus:

    setTimeout(function(){ alert("later"); }, 300);
Or, on multiple lines:

    setTimeout(-> 
      alert "later"
    , 300)

    setTimeout(function(){ 
      alert("later"); 
    }, 300);


But here we're introducing brackets just to avoid the syntax ambiguity. The Pythonista in me is crying out for a single way of doing things, either require brackets or don't. It makes me want to go down the road of always using parenthesis in CoffeeScript, but then it'll feel like I'm not using the language in an idiomatic way (which is something I hate doing).


In my mind, this usage of parenthesis is less ambiguous. Parenthesis are used in C-like languages for both function calls and order of operations. That is to say, they are used in these two ways:

    foo(a, b, c)  // functions
    (a + b) * c   // order of operations
Making parenthesis optional for function calls means parenthesis are used instead for just controlling order of operations.

In practice, CoffeeScript can't always guess when something is a function call (such as a function call without arguments), so you end up mixing in some function call usage no matter what.


I really hope it isn't guessing anything...


Personally, I prefer

    setTimeout -> (
      alert "later"
      alert "later"
      alert "later"
      ), 300
to

    setTimeout(-> 
      alert "later"
      alert "later"
      alert "later"
      , 300)

I think it's much more clear and close to how we write JS code:

    setTimeout(function() {
      alert("later");
      alert("later");
      return alert("later");
    }, 300);


In my experience I would find myself frequently falling back to explicit brackets and more traditional Javascript syntax when I wasn't sure how to construct the statement. Which was okay (I wasn't stumped), but it happened fairly often. Your multiple line example (without "function") is the kind of thing I wouldn't be able to create on my own – both the indentation and comma have to be in just the right place, and the rules that govern that are not very clear. Or rather, it seems to be the one thing that works given a bunch of rules that are interacting in that particular situation. And each time I'd get something like that wrong it'd probably compile, but then I'd have to read the generated source to see what I'd actually done.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: