Merge remote-tracking branch 'redux/master' into sh4-pool
[tamarin-stm.git] / esc / es4-doc / rest-arguments.txt
blobb27f44bc9b705067132dd698ab2e0c85a18a9d8c
1 NAME:       "Rest parameter"\r
2 CATEGORY:   ?\r
3 SOURCES:    ?\r
4 AUTHOR:     Lars\r
5 STATUS:     ?\r
6 REVIEWS:    (none)\r
7 RI STATUS:  ?\r
8 ESC STATUS: ?\r
9 ESC TEST:   ?\r
12 DESCRIPTION\r
14 A formal parameter list that appears as part of a function definition\r
15 (including constructor functions) or function expression can contain\r
16 an additional, final element called a "rest parameter".  Syntactically\r
17 it is comprised of the token "..." (an sequence of three ASCII full\r
18 stop characters (U+002E) not separated by white space) followed by a\r
19 single identifier.\r
21 Upon activation the identifier will be bound as a variable in the\r
22 activation object in the same fashion as the other formal parameters.\r
23 The value stored in the variable will be a fresh Array object.\r
25 If there are n formal parameters preceding the rest parameter and m\r
26 actual arguments passed to the function activation, then arguments\r
27 n+1, n+2, and so on up through m will be stored in properties 0, 1,\r
28 and so on up through m-n-1 in the Array object, and the "length"\r
29 property of the Array object will be initialized to m-n.  (If m < n\r
30 then no propeties are stored and "length" is 0.)\r
32 The use of a rest parameter precludes the use of the "arguments"\r
33 object in the function.  If the parameter list calls for a rest\r
34 parameter then no "arguments" object will be created and the local\r
35 variable "arguments" will not be implicitly bound on function entry.\r
38 EXAMPLE\r
40 The output of this program:\r
42   function f(p1, p2, ...rest) {\r
43     print(rest.length);\r
44     for ( let i=0 ; i < rest.length ; i++ )\r
45       print(rest[0]);\r
46   }\r
48   f(1,2,3,4,5)\r
50 are the numbers 3, 3, 4, and 5.\r
53 FUTURE CONSIDERATIONS\r
55 We have debated two facilities for type annotation:\r
57   - It might be desirable to make it possible to declare the type of\r
58     the rest parameter as "Array", eg\r
60        function f(p1, p2, ...rest: Array) \r
62   - If array structural types are incorporated into ES4 then it will\r
63     be natural to allow an array-structural type to annotate the rest\r
64     parameter, giving a type to the array elements.\r
66 I have not incorporated these into the proposal yet because I think it\r
67 would be premature to do so.\r
70 REFERENCES\r