Skip to content

Instantly share code, notes, and snippets.

@aseemk
Created March 18, 2013 23:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aseemk/5191967 to your computer and use it in GitHub Desktop.
Save aseemk/5191967 to your computer and use it in GitHub Desktop.
Sexy method argument overloading in CoffeeScript.
#
# Overloads:
# - fetch(url, callback)
# - fetch(url, opts, callback)
#
fetch = (args...) ->
[arg0, arg1, arg2] = args
[url, opts, callback] = switch args.length
when 0, 1 then throw new Error "Usage: fetch(url[, opts], callback)"
when 2 then [arg0, null, arg1]
else [arg0, arg1, arg2]
# ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment