function length for splatted function.
In JS, the length of the function "Specifies the number of arguments expected by the function. "[1], hence
f3 = (a, b, c) ->
f1 = (a) ->
f0 = ->
console.log f3.length #3
console.log f1.length #1
console.log f0.length #0
However,
fs = (a, b..., c) ->
console.log fs.length # 0
Is zero itended for splatted function? If not, what's the value?
I think arity should be 0 for var-args functions. If that's not acceptable, I think it should be the number of fixed arguments (2 for fs above)
I thought this was wrong too.. when I considered the case of
(a,b,c,args...) ->
which could be 3.
But I don't see an obvious arity for
(a,b,args...,c,d)
is it 2, or 4?
@adam-f: Yeah, that's a nonobvious choice. I'd be okay with 4 but not with 2. That's another reason to support 0.
Actually, I'm thinking if there's a point on defining what the length of the function should be in CS, after all, unlike JS, CS has default value for argument and splatted argument, so I wonder if it really make sense to maintain the length of a function.
Yes, it makes much less sense to talk about the "length" of a function in CoffeeScript.
(one, two, three = 3) -> ...
Could be either two, or three, arguably. I don't think we need to worry about this too much. In addition, outside of really fancy metaprogramming, the length of a function is hardly ever used in JS.
I'm not in favor of reopening this issue, but I'm not sure if the binding between args and their counterparts in the arguments object was taken into consideration.
An arg is only mapped to the arguments object if its index within arguments is less than or equal to the function's arity. ES5 Spec, 10.6 Arguments Object, 11.c. Since splatted functions have a different arity when compiled to JavaScript (always 0), this mapping never takes place:
# splatted; arity of 0
( (a,b,c...) -> arguments[2] = 4; alert c )(1,2,3) # alerts 3
# not splatted, arity of 3
( (a,b,c) -> arguments[2] = 4; alert c )(1,2,3) # alerts 4
# splatted; arity of 0
( (a,b,c...) -> c = 4; alert arguments[2] )(1,2,3) # alerts 3
# not splatted; arity of 3
( (a,b,c) -> c = 4; alert arguments[2] )(1,2,3) # alerts 4
While there are many instances of assigning to arguments, I couldn't find any code that relied upon the arg reflecting its arguments counterpart. It looks like jQuery has some code to explicitly tie an arg to arguments:
event = arguments[0] = jQuery.event.fix( event || window.event ); // `event` is the function's single parameter name
Using that strategy, the examples above could easily be rewritten:
( (a,b,c...) -> c = arguments[2] = 4; alert "#{c}:#{arguments[2]}" )(1,2,3) # alerts "4:4"
It should be noted that strict mode doesn't entangle args and the arguments object under any circumstances.
The only solution to the arity issue would be to require declaring any splats as the final parameter. I would think developers depend on inner splatted params more than entangled arguments.