Skip to content

Instantly share code, notes, and snippets.

@jimmycuadra
Created September 14, 2011 04:39
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jimmycuadra/1215863 to your computer and use it in GitHub Desktop.
Save jimmycuadra/1215863 to your computer and use it in GitHub Desktop.
Syntax question for CoffeeScript
# Given a function that accepts a function followed by a non-function argument...
foo = (callback, name) ->
console.log "Calling callback #{name}..."
callback()
# you end up with a line starting with
# a comma which just feels wrong...
foo ->
console.log "Callback called!"
, "bar"
# or an awkard set of parens which generates
# unnecessary parens in the compiled source...
foo (->
console.log "Callback called!"
), "bar"
# or a syntax error.
foo ->
console.log("Callback called!"), "bar"
# Wat do? What's the preferred way to write this?
@jrus
Copy link

jrus commented Dec 22, 2011

I have to admit though that I do find it a bit inconsistent/odd that when arguments are an object (key/value), indentation alone is enough to indicate calling:

somefunc
    foo: '1'
    bar: '2'

but when they’re just positional arguments (whether functions or not), there’s a requirement to either add parentheses, or put one argument and a comma on the same line as the call:

somefunc(
    'yay'
    (spam) -> eggs
)

or:

somefunc 'yay',
    (spam) -> 'eggs'

I’d personally prefer if this was also interpreted as a call instead of a syntax error:

somefunc
    'yay'
    (spam) -> 'eggs'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment