fail-fast object destructuring

Brendan Eich brendan at mozilla.org
Tue Jul 10 07:38:39 PDT 2012


Russell Leggett wrote:
> On Mon, Jul 9, 2012 at 4:04 PM, Brendan Eich <brendan at mozilla.org 
> <mailto:brendan at mozilla.org>> wrote:
>
>     Russell Leggett wrote:
>
>         Options type objects:
>             let {url,callback,?errback} = options;
>         This lets you very clearly decompose an object and
>         declaratively show which values are optional and which ones
>         are not. If all of them are optional, the ? would just go to
>         the left of the brace, but really this should be the minority
>         case for most situations.
>
>
>     Dave and I had talked about prefix-? as applying only to property
>     names, since that covers all but the top level for object patterns:
>
>       {required, ?optional_shallow}
>       {required, ?optional_deep: {foo, bar, ?baz}}
>
>     but of course it leaves out the top level.
>
>
> This makes a lot of sense. It is certainly smaller. I do worry, 
> though, that it might fall short for the people that would like 
> irrefutable destructuring. I had imagined that ?{a,b,c} could be used 
> in a case where all properties would be optional.

Yup. But as your previous mail noted, that might be a rare (hard) case 
and hard cases make bad law. Or so they teach law students!

> I did have a question about refutability. What would happen here?
>
>    let {?a,?b} = null;
>
> Even though both properties are optional, would this still fail? Does 
> the {} imply some structure?

This is the separate ToObject vs. must-be-of-spec-Object-type issue. JS 
in SpiderMonkey, Rhino, probably other engines today:

js> let {a,b} = null
typein:1: TypeError: null has no properties
js> let {a,b} = undefined
typein:2: TypeError: undefined has no properties
js> let {a,b} = 42
js> a
js> b

We need to resolve this one independently from the presence of prefix-? 
on the left.

>     Right, this minority case could be left out. The array pattern
>     situation is stronger motivation for prefix-? applying to bindings
>     not property names.
>
>
> Yes, I think that arrays are likely the strongest case for ? on 
> bindings. Something like this would be nice:
>     let [first,last,?company] = contact.split(",");
>
> And certainly, if you wanted to be able to destructure a list of 
> arguments, some of which being  optional, it would be very useful.
>
> Ultimately, while I think I could personally do fine with fewer points 
> of prefix-?, I can see a few cases for it, especially on arrays. I 
> worry that without supporting these, refutable by default won't be 
> able to reach consensus.

I bet you are right. We'll be taking this up in a couple of weeks, it's 
on the agenda. Thanks for your posts on the whole issue.

/be


More information about the es-discuss mailing list