nested destructuring of undefined and default parameters

# Aaron Frost (12 years ago)

I am trying to understand what should happen if you do a nested destructuring of undefined, where the pattern has a default value included. Here is an example of my question:

var foo = { bar : { baz : true } }; function readFoo({ bar: { baz="DEFAULT BAZ"} }){ console.log(baz); } readFoo(foo); //true readFoo(undefined); //what should happen here

Should the second call error because it can't find property baz of undefined bar (undefined.baz)? Or should is assign the default because it couldn't locate the value with no error? Possible third option?

-- AF

# Andreas Rossberg (12 years ago)

On 5 February 2013 14:32, Aaron Frost <aaronfrost at gmail.com> wrote:

I am trying to understand what should happen if you do a nested destructuring of undefined, where the pattern has a default value included. Here is an example of my question:

var foo = { bar : { baz : true } }; function readFoo({ bar: { baz="DEFAULT BAZ"} }){ console.log(baz); } readFoo(foo); //true readFoo(undefined); //what should happen here

Should the second call error because it can't find property baz of undefined bar (undefined.baz)? Or should is assign the default because it couldn't locate the value with no error? Possible third option?

That would throw a type error, in both the semantics currently in the draft spec as well as the modified "refutable" semantics that was discussed at the last meeting (harmony:refutable_matching). With what is proposed on the wiki, however, you can actually make this succeed by explicitly marking the whole pattern as "soft" with a '?':

function readFoo(?{ bar: { baz = "DEFAULT BAZ"} }) { console.log(baz); }

readFoo(undefined); // DEFAULT BAZ

# Aaron Frost (12 years ago)

my apologies for not reading that ahead of time.

I just took the time to read it and must say that I really like this. The syntax is easy to read and offers flexibility. Kudos to all who contrib'd to this.

AF