Parameter lists as arguments destructuring sugar

Sean Eagan seaneagan1 at gmail.com
Fri Apr 8 10:02:41 PDT 2011


Actually, forget about desugaring.  Here is a refined set of proposals
to replace what's above:


Define parameter lists in terms of array destructuring:
===============================================================

Why:

Parameter lists already perform limited destructuring of function
arguments, why not give them full array destructuring power,
especially with the eventual deprecation of |arguments|?.  Conversely,
array destructuring could benefit from default values.  Also, the
principle of least surprise would suggest that the two should be
consistent with each other, which this proposal would guarantee, even
if extensions (such as those proposed below) were to be added ?

How:

  ES5 spec:

    Replace section 10.5 step 4, b-f with...

      b. Call env's DestructureArray concrete method passing args,
names, and strict as the arguments.

    ...where "DestructureArray" refers to step 3 in the algorithm on
the destructuring page [1].

  Wiki:

    Merge the parameter default values page [2] into the destructuring page.

    Remove the rest parameters page [3], and call out rest parameters
as an example use case on the destructuring page.


Potential destructuring extensions
===============================================================

Some of this depends on the first proposal, 3 is new, 2 (except 2ai)
may already be allowed due to how LValue is specified in [1], not
sure...

  LValue  ::= <any lvalue expression allowed in a normal assignment expression>


  1) Allow the optional spread operator element (aka rest parameter)
and any default values to occur anywhere in array destructuring
patterns (including parameter lists), not just at the end.

    a) spread operator element does not start getting filled up until
all other elements are filled up, including those after it
    b) make identifier for spread operator element omittable, as is
true for any other element

    function(important, ..., alsoImportant) {}
    [important, ..., alsoImportant] = arr;

    c) if an element does not have an explicit default value, then it
has an implicit |undefined| default value

    function(x = 5, y){}
    [x = 5, y] = arr;

  2) Allow var, let, const, and any future modifiers inside
destructuring patterns, overriding any outside modifiers

    let [a, const b, var {x, let "y": z}] = arr;

    a) This would also allow for alternate modifiers for parameters

      function(const x, const y) {}

      i ) Maybe sharp function [4] parameters should be let scoped by
default rather than var?

        #(x, y) {/*x and y are let scoped here*/}

  3) Allow default values in object destructuring

    {first = 'unknown', last = 'unknown'} = name;
    {first: firstName = 'unknown', last: lastName = 'unknown'} = name;


Changes from ES5 needed for parameter lists
===============================================================

In section 10.6, add logic to map between rest parameter elements to
|arguments| elements, and to ignore omitted parameters.

Update section 15.3.2.1, allow for arbitray parameter list to be used,
possibly just let the first parameter be the entire parameter list...

  let f = new Function(
    "this = defaultThis| [a, {b}], ...c",
    "//..."
  );


[1] http://wiki.ecmascript.org/doku.php?id=harmony:destructuring
[2] http://wiki.ecmascript.org/doku.php?id=harmony:parameter_default_values
[3] http://wiki.ecmascript.org/doku.php?id=harmony:rest_parameters
[4] http://wiki.ecmascript.org/doku.php?id=strawman:shorter_function_syntax

Thanks,
Sean Eagan


More information about the es-discuss mailing list