fail-fast object destructuring (don't add more slop to sloppy mode)

Herby Vojčík herby at
Wed Jan 2 07:02:03 PST 2013

Brendan Eich wrote:
> I think the refutable-by-default patterns need a ? modifier only for a
> "leaf" identifier in a pattern, not for non-terminal identiifers. For
> example:
> let {p: {q: r}} = deep_o;
> wants to get o.p.q and bind it to r.
> With the current irrefutable design, if deep_o = {} or another object
> lacking a {q:...} sub-object named p, then this example will fail with a
> TypeError even though a shallow example fails-soft (or fails slowly,
> really: fails later) by pulling undefined out as the value of p and
> binding it to the local (as in the first example in this post).
> With a refutable-by-default design, however, it seems to me nonsensical
> to support ? on p as well as q in this example:
> let {p?: {q?: r}} = deep_o;
> because if deep_o has no property p, what should happen? No binding r at
> all? Or a let binding named r with value undefined? What if there's no ?
> after q in this case -- should that matter?\
> We already rejected CoffeeScript's existential operator because of its
> lack of compositionality. See
> (the section starting "ARB: This is non-compositional"). I think the
> same objection applies to allowing p? in the deep_o case.

I advised adding Null Pattern Object into language in the aforementioned 
thread; and use foo? as `foo == null ? NullPatternObject : foo`.

So you can do `r = o?.p.q`; and it does not fail.
With such construct you could do
   let {p: {q: r}} = deep_o?;

> Comments welcome. I'll put this on this month's TC39 meeting agenda.
> /be


More information about the es-discuss mailing list