Parameter lists as arguments destructuring sugar

Bob Nystrom rnystrom at google.com
Fri Apr 8 11:02:05 PDT 2011


I've implemented an unrelated language that works this way, and it is really
nice, but I don't know if moving ES to that after the fact would work. But
maybe I'm misreading your proposal. Consider:

  function show(a, b, c) { log(a + " | " + b + " | " + c); }
  var arg = [1, 2, 3];
  show(arg);

Currently, that prints "1, 2, 3 | undefined | undefined".

With your proposal, would that destructure arg on entry and print "1 | 2 |
3"? Or is the idea that a function call implicitly array-ifies its argument
list to avoid that, so "show(arg)" implicitly is semantically "show([arg])"
and you get the original behavior?

Assuming the above is resolvable, would you also be able to use object
destructuring in a function parameter list? I.e.:

  function show(x: x, y: y) { log(x + ", " + y); }
  var point = { x: 1, y: 2 };
  show(point); // "1, 2"

- bob

On Fri, Apr 8, 2011 at 10:02 AM, Sean Eagan <seaneagan1 at gmail.com> wrote:

> 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
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20110408/cad98fae/attachment.html>


More information about the es-discuss mailing list