tc39/proposal-optional-chaining

assigments (creating missing objects in the path)

giorgio-zamparelli opened this issue · 3 comments

the optional chaining is very similar to the path in ramda
R.path(['a', 'b'], {a: {b: 2}}); //=> 2
R.path(['a', 'b'], {c: {b: 2}}); //=> undefined

in ramda there is another function complementary to path for assignments called assocPath.
R.assocPath(['a', 'b', 'c'], 42, {a: 5}); //=> {a: {b: {c: 42}}}

That essentially creates missing intermediary objects.

Do you think the assignment could be part of this proposal?
var container = {a: 5}
container.a?.b?.c = 42; //=> {a: {b: {c: 42}}}

See #18.

noppa commented

Optional assignment as discussed in #18 doesn't do anything when LHS has a null/undefined.
Optional setter functions in ramda and lodash actually create missing intermediate properties if needed.

I'm highly skeptical this kind of "works as intended 80% of the time" magic behavior would ever get its way to the language, especially considering that the more reasonable proposal didn't fly either.

Thanks for explaining this even though it was a duplicate of #18