-
-
Save jashkenas/1790643 to your computer and use it in GitHub Desktop.
# Apart from the basic niceties of being able to leave off | |
# parens in clear calls... | |
print 10 | |
alert "Hello #{name}" | |
# ... the reason why they're important to have in CoffeeScript | |
# is because it allows our "block" syntax (single function body | |
# passed as the final argument to a call) to play nicely with | |
# significant whitespace: | |
fs.readFile "config/options.json", (err, contents) -> | |
... work with file here. | |
# Writing the above without the ability to leave off the | |
# parens which would otherwise have to wrap the function body | |
# would defeat a large part of the purpose of significant | |
# whitespace in the first place. |
If you don't like the HEREDOC-inspired syntax (understandable; that form of HEREDOC wigs a lot of people out), how about this. An alternate method-call syntax with very low binding priority. I'll use ..
for the sake of example:
$ 'some_selector' .. click (e) ->
...click handler...
The latter of your two examples is what I used, but I share your distaste for the trailing paren in a language that tries to avoid them. The former is obvious but not really helpful, since I'm looking for syntactical fixes, not code restructurings. What do you think of a low-precedence method calling syntax for chaining without parens?
Ah, interesting. An explicit "close" operator that ends implicit calls, without having to start them... That would be great material for a Github ticket, if you feel like writing it up and starting discussion. I also don't think it's something we've ever talked about before (a true rarity for CoffeeScript syntax proposals).
Another way to avoid the inconsistency is of course:
($ 'some selector').click (e) ->
...
In my Ruby example, I'm pointing out that Ruby has to have a special case for the syntax we're talking about here, one which doesn't behave consistently with passing a function expression as an argument... We can talk about the same Ruby inconsistencies with other block forms:
Whereas in CoffeeScript:
... but in any case, to answer your question -- if you value visual consistency for that call, I'd recommend either of these two options:
... both of which work fine.