This proposal has progressed to the Draft ECMAScript 6 Specification, which is available for review here: specification_drafts. Any new issues relating to them should be filed as bugs at http://bugs.ecmascript.org. The content on this page is for historic record only and may no longer reflect the current state of the feature described within.

Background

Goals

  • Provide a better arguments so we can deprecate, obsolete, and some years hence burn with fire / salt the earth anything to do with arguments, foo.arguments, etc.
  • Capture trailing arguments after one or more fixed formal parameters

Sketch

  • Allow ... as an optional prefix before the last formal parameter in a function declaration
  • Presence of ... in function foo(...rest) {...} creates a rest parameter, rest, denoting a new Array created when foo is called, whose elements from 0 to foo.length are supplied by the actual arguments passed to foo (if any)
  • Similarly for function bar(baz, bletch, ...rest) {...}, with elements of rest being supplied by the third through last actual parameters
// Trivial this-free wrapper around Function.prototype.apply:
function simpleApply(func, ...args) {
    "use strict";
    return func.apply(undefined, args);
}

// Simple printf-style string formatter, write and convert left as exercises:
function printf(format, ...args) {
    var n = 0;
    var i = 0, j;

    while ((j = format.indexOf('%', i)) >= 0) {
        if (n >= args.length)
            throw "printf: too few args for given format " + format;
        write(format.slice(i, j));

        // convert takes exactly one format-specifier char, to keep things simple.
        write(convert(s[j + 1], args[n++]));
        i = j + 2;
    }
    write(format.slice(i));
}

Syntax

The FormalParameterList clause needs to be updated as follows:

FormalParameterList :
  ... Identifier
  FormalParameterListNoRest
  FormalParameterListNoRest , ... Identifier
     
FormalParameterListNoRest :
  Identifier
  FormalParameterListNoRest , Identifier

Semantics

We let all Function objects have a boolean internal property indicating if it has rest parameters.

Table 9 - Internal Properties Only Defined for Some Objects

Internal Property Value Type Domain Description
[[HasRestParameter]] Boolean Whether the function was declared with a rest parameter. The last parameter in the [[FormalParameters]] List is used for the rest parameter. Of the standard built-in ECMAScript objects, only Function objects implement [[HasRestParameters]].

10.5 Declaration Binding Instantiation

  1. Let env be the environment record component of the running execution context’s VariableEnvironment.
  2. If code is eval code, then let configurableBindings be true else let configurableBindings be false.
  3. If code is strict mode code, then let strict be true else let strict be false.
  4. If code is function code, then
    1. Let func be the function whose [[Call]] internal method initiated execution of code. Let names be the value of func’s [[FormalParameters]] internal property.
    2. Let hasRest be the value of func’s [[HasRestParameters]] internal property.
    3. Let namesCount be the number of elements in names.
    4. Let argCount be the number of elements in args.
    5. Let n be the number 0.
    6. For each String argName in names, in list order do
      1. Let n be the current value of n plus 1.
      2. If hasRest is true and n is equal to namesCount
        1. Let v be the result of creating a new object as if by the expression new Array() where Array is the standard built-in constructor with that name.
        2. Let index be 0.
        3. Repeat while n less than or equal to argCount
          1. Let value be the value of the nth element of args.
          2. Let indexName be ToString(index).
          3. Call the [[DefineOwnProperty]] internal method of v with arguments indexName, the Property Descriptor { [[Value]]: value, [[Writable]]: true, [[Enumerable]]: true, [[Configurable]]: true}, and false.
          4. Let index be the current value of index plus 1.
          5. Let n be the current value of n plus 1.
      3. Else
        1. If n is greater than argCount, let v be undefined otherwise let v be the value of the nth element of args.
      4. Let argAlreadyDeclared be the result of calling env’s HasBinding concrete method passing argName as the argument.
      5. If argAlreadyDeclared is false, call env’s CreateMutableBinding concrete method passing argName as the argument.
      6. Call env’s SetMutableBinding concrete method passing argName, v, and strict as the arguments.
  5. ...

13.2 Creating Function Objects

The callers of 13.2 should pass in hasRest which indicates that the function has a rest parameter.

  • Set the [[HasRestParameters]] to hasRest.

Issues

  • Is there any benefit to allowing ... with no formal parameter identifier after it? Not without types or arity checks.
  • Should rest parameters be frozen? There is no aliasing hazard, so freezing is premature and limits utility, IMHO.

Brendan Eich 2009/05/04 22:52

The length property of a function should be updated to not include rest parameters.

Erik Arvidsson 2010/11/05 22:42

The semantics is not yet complete. It needs to get whether FormalParameterList contains rest parameters and forward that to 13.2.

Erik Arvidsson 2010/11/09 20:47

Functions that define a rest parameter should not also have an arguments object. In other words, using ... in the parameter lists should prevent the creation of the arguments object and the local binding of “arguments”. Should the strict mode restrictions on the use of the identifier “arguments” be relax in that case?

Allen Wirfs-Brock 2010/11/10 00:17

 
harmony/rest_parameters.txt · Last modified: 2013/07/11 23:59 by rwaldron
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki