length property value for functions with parameter enhancements
Allen Wirfs-Brock
allen at wirfs-brock.com
Fri Aug 26 15:34:55 PDT 2011
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. (See http://wiki.ecmascript.org/doku.php?id=harmony:parameter_default_values and http://wiki.ecmascript.org/doku.php?id=harmony:rest_parameters ). Remember that according to 15.3.5.1the value of the length property of a function is "the 'typical' number of arguments expected by the function" and that for user defined functions, the value of the length property as currently specified in 13.2 is the number of items in the functions formal parameter list.
For built-in, section 15 says that the length property value for built-ins "unless otherwise specified, this value is equal to the largest number of named arguments shown in the subclause headings for the function description, including optional parameters". However, there are many cases where the length is "otherwise specified" and in many of those the pattern seems to be to not include optional parameters in the length.
Some examples,
Built-in constructors most commonly have a length of 1. This includes Array which can be viewed as using a rest parameter. However, Date has a length of 7 reflecting that it can have up to 7 optional parameters and RegExp has a length of 2 even though its second argument is arguably optional (although it isn't shown as such in the section heading). But overall, these generally the follow the section 15 default rule of counting optional parameters.
Array.prototype and String.prototype functions generally don't include optional parameters in the length count and they generally give a function that only has a single rest parameter (for example, concat and push) a length of 1 but don't include any rest parameters in the length for functions such as splice where the rest parameter is not the firs parameter.
Date.prototype has a number of functions that take optional arguments and they are all counted in the length.
(BTW, it would be a useful exercise for somebody to try to recode all the built-in function section headings using optional/rest parameter syntax)
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
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?
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
Thoughts?
Allen
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20110826/45b1643f/attachment.html>
More information about the es-discuss
mailing list