length property value for functions with parameter enhancements
Allen Wirfs-Brock
allen at wirfs-brock.com
Sun Aug 28 16:36:47 PDT 2011
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:
>> Something we need to define for ES.next is how to compute the length
>> property value for functions whose formal parameter list includes "optional"
>> and/or rest parameters.
>
> True, and actually, there are more issues with length & function
> proxies. I don't have my notes with me right now, but for example, it
> is not clear at all what length Function.prototype.bind should set
> when called on a function proxy. 0? 1? Should it try to get the length
> property from the proxy and subtract N? What if length is not defined
> on the proxy, or not a (natural) number?
The ES5.1 spec. defines how how bind determines the length for the function it creates based upon the length property of the target function. I would expect the same rules would apply when the target is a function proxy.
>
> This is probably something the proxy proposal has to resolve
> eventually, but it's worth keeping in mind for the broader picture.
>
>> So, what is a length determination algorithm that recognizes optional/rest
>> arguments and is consistent with the stated intent of length and (as much as
>> possible)existing section 15 definitions. Here is one proposal:
>> The length is 0 only if the formal parameter list is empty.
>> For example:
>> function foo() {}; //foo.length==1
>
> You meant ==0 here, right?
right
>
>> 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,
>
>> 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.
>
> FWIW, one could also argue for setting length to +infinity for
> functions with only rest parameters. :)
But there is no precedent for that and surely infinity is not the "typical" number of arguments.
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.
Allen
More information about the es-discuss
mailing list