length property value for functions with parameter enhancements

Brendan Eich brendan at mozilla.com
Mon Aug 29 13:29:40 PDT 2011


On Aug 28, 2011, at 4:36 PM, Allen Wirfs-Brock wrote:

> On Aug 27, 2011, at 6:12 AM, Andreas Rossberg wrote:
> 
>> On 27 August 2011 00:34, Allen Wirfs-Brock <allen at wirfs-brock.com> wrote:
>>> If the formal parameter list includes any non-optional, non-rest formal
>>> parameters, the length is the total number of non-optional/non-rest formal
>>> parameters.
>>> For example:
>>>    function bar(a,b,c) {} //bar.length=3
>>>    function baz(a,b,c=0) {} //baz.length=2
>>>    function bam(a,b,c=0,...d) {} //bam.length==2   BTW, is this legal?
>> 
>> Makes sense. (And yes, I don't see why the latter shouldn't be legal.)
> Because the is a  potential for misinterpreting the user intent on such a call.  For
>    bam('a','b',1,2,3) 
> we surely have to interpret the argument/parameter mapping as:
>     a='a',b='b',c=1,d=[2,3] 
> but it is easy to imagine a programmer intending
>     a='a',b='b',c=0,d=[1,2,3] 
> Making it illegal to have a formal parameter list that has both optional and result parameters might result the likelihood of that confusion,  

That's too restrictive. There are lots of ways to go wrong in JS, we don't try to put programmers in a five-point harness.

Python:

>>> def foo(a,b,c=1,*d):
...   print(a, b, c, d)
... 
>>> foo(0,1,2,3,4)
0 1 2 (3, 4)
>>> foo(0,1,2,3)
0 1 2 (3,)
>>> foo(0,1,2)
0 1 2 ()
>>> foo(0,1)
0 1 1 ()

(Python reflects rest params [splats] as tuples not lists.)

Ruby is of course pretty similar:

def foo(a,b,c=1,*d)
  print a, b, c, d, "\n"
end
foo(0,1,2,3,4)
foo(0,1,2,3)
foo(0,1,2)
foo(0,1)
^D
01234
0123
012
011

(My Ruby REPL skills are sad -- ugly run-together stdout follows the TTY EOF ^D above.)


>>> If there are no non-optional or non-rest formal parameters the length is 1.
>>>    function bar1(a=0) {} //bar1.length=1
>>>    function baz1(a=0,b=1,c=2) {} //baz1.length=1
>>>    function bam1(...a) {} //bam1.length==1
>> 
>> I'm not so sure about this, it seems incoherent. Why not 0, especially
>> for the first two? You mentioned builtins like Array above, but I
>> would rather count them as the exception to the rule (especially given
>> that the builtins don't seem entirely consistent wrt length anyway).
> 
> In my proposal, I am decided to make a clear distinction between truly empty  formal parameter lists and  those with only various forms of optional formal parameters by only giving a 0 length to the empty formals case.  That's a debatable decision but it seems desirable to distinguish the two cases and the built-ins is the only precedent that we have to follow.

You make a distinction but create an ambiguity between

function foo(a=0)... // length 1
function bar(a=0,b=1) // length 1

That seems no more consistent (and less coherent as Andreas put it). If trailing optional parameters do not contribute to length, then we have a consistent single rule for length. That it does not match all built-ins is sad history, not to be rewritten lightly but not to overdetermine the future.


> I'm not sure if there is any real use case for the length property of ECMAScript functions.  Does anybody know of one? Regardless, I do think we can get rid of it.

Working on instrumentation to find uses on the web. More in a bit,

/be


More information about the es-discuss mailing list