Patterns in parameter declarations: irrefutable?

Andreas Rossberg rossberg at google.com
Fri Mar 22 06:28:25 PDT 2013


On 20 March 2013 23:49, Axel Rauschmayer <axel at rauschma.de> wrote:
> Thanks, didn’t know that applied to parameters, too. Then the following two
> functions are equivalent(?)
>
>     function (arg0, arg1, arg2) {
>         // ...
>     }
>     function (...args) {
>         let [arg0', arg1', arg2'] = args;
>         // ...
>     }
>
> With the following rules for translating argi -> argi':
> - paramName -> ?paramName
> - pattern -> pattern (rest parameters, parameter default values, curly
> braces, square brackets)

Not quite. In the latter form, default expressions could see all local
declarations from the function body, which we decided a couple of
meetings ago should not be the case for the former. More pedantically,
'paramName' should probably include parenthesized identifiers, and
'pattern' sloppy patterns ?pattern. (The latter also introduces a new
variable 'args' into the scope and produces a different length
property for the function, but let's ignore that.)

The more accurate desugaring probably is

  function f(pat1, ..., patN, ...patR) { body }

into

  function f() {
    let pat1 = arguments[0]
    ...
    let patN = arguments[N-1]
    let [?*, ..., ?*, ...patR] = arguments
    return (function(){ body })(...arguments)
 }

(still ignoring the length property, and taking the liberty of using *
as a wildcard pattern to avoid inventing variable names).

/Andreas


More information about the es-discuss mailing list