Patterns in parameter declarations: irrefutable?

Axel Rauschmayer axel at rauschma.de
Wed Mar 20 15:49:13 PDT 2013


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)


On Mar 20, 2013, at 22:45 , Brendan Eich <brendan at mozilla.com> wrote:

> The proposal we accepted, written by Andreas at http://wiki.ecmascript.org/doku.php?id=harmony:refutable_matching, seems clear. Patterns are refutable without an extra ? annotation. Andreas is using prefix-?, note well.
> 
> /be
> 
> Axel Rauschmayer wrote:
>> Are patterns in parameter declarations irrefutable? In other words, given:
>> 
>>    function foo({ opt1 }) { return opt1 }
>> 
>> What happens if we call foo()?
>>    1. An exception is thrown
>>    2. undefined is returned
>> I’d expect (2). If opt1 had a default value, I’d expect that value to be returned.
>> 
>> What happens if we call foo({})?
>>    1. An exception is thrown
>>    2. undefined is returned
>> 
>> I’m asking, because I’ve seen the pattern
>> 
>>    function foo({ opt1 = 3 } = {}) { return opt1 }
>> 
>> If explicitly marking something as irrefutable is necessary, I’d prefer:
>> 
>>    function foo({ opt1 = 3 }?) { return opt1 }
>> 
>> 
>> It’s a bit of a shame that destructuring and parameter declarations diverge, but I guess it can’t be helped.

-- 
Dr. Axel Rauschmayer
axel at rauschma.de

home: rauschma.de
twitter: twitter.com/rauschma
blog: 2ality.com

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20130320/f080b074/attachment-0001.html>


More information about the es-discuss mailing list