Skip to content

Instantly share code, notes, and snippets.

@geraldalewis
Created January 13, 2012 19:55
Show Gist options
  • Save geraldalewis/1608379 to your computer and use it in GitHub Desktop.
Save geraldalewis/1608379 to your computer and use it in GitHub Desktop.
default arguments order
# non-default params = true arity;
# args <= arity: apply args to non-defaults first, in order
# args > arity: apply args in order
f = (a=0,b,c=2) ->
f() # a = 0 b = undefined c = 2
f(1) # a = 0 b = 1 c = 2
f(1,2) # a = 1 b = 2 c = 2
f(1,2,3) # a = 1 b = 2 c = 3
f = (a=0,b,c,d=3) ->
f() # a = 0 b = undefined c = undefined d = 3
f(1) # a = 0 b = 1 c = undefined d = 3
f(1,2) # a = 0 b = 1 c = 2 d = 3
f(1,2,3) # a = 1 b = 2 c = 3 d = 3
f(1,2,3,4) # a = 1 b = 2 c = 3 d = 4
@michaelficarra
Copy link

Ew. I would not like this behaviour.

@geraldalewis
Copy link
Author

Hah! I initially wrote 'ick' at the bottom. I had thought this behavior would be handy a few times recently, and asked jashkenas and brendaneich on twitter:

f = (a=1,b,c=3) ->
f(2) # a = 1, b = 2, c = 3

brendaneich said "what about f(1,2)". The above was me reasoning out how it'd work and then filing it away here so that I would remember why it wasn't a good idea to propose it for CoffeeScript ;)

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