tc39 / proposal-optional-chaining

https://tc39.github.io/proposal-optional-chaining/
4.94k stars 75 forks source link

Optional spread syntax #55

Closed justinfagnani closed 5 years ago

justinfagnani commented 6 years ago

Spread for arrays and function calls is not null-safe (though it is for object spread).

It'd be very convenient to write them as:

const arr = [...?listOne, ...?listTwo];
foo(...?args);

Instead of:

const arr = [...listOne || [], ...listTwo || []];
foo(...args || []);
claudepache commented 6 years ago

Indeed, but it is out of the scope of this proposal. I could add a note in the explainer for “possible future improvements”.

(It is neither a pressing issue, since || [] just works.)

ljharb commented 6 years ago

Also, with the nullish coalescing operator, I think foo(...args ?? []) and [...(listOne ?? [])] would precisely match the semantics you'd want ...? to have?

justinfagnani commented 6 years ago

It is neither a pressing issue, since || [] just works.

I agree it's not as pressing as the others, but the same just works bit could be said about them: a?.b is roughly (a || {}).b if you're expecting only an object or nullish value, or exactly (a ?? {}).b with nullish coalescing. But, if we thought this addition would slow anything down at all, I'd say definitely wait - the current 3 are far more important. Seems like the basic syntax is the big delay risk there currently though.

I think foo(...args ?? []) and [...(listOne ?? [])] would precisely match the semantics you'd want ...? to have

I would think in this case we could elide the empty array allocation completely.

ljharb commented 6 years ago

You could use an empty string as well, but i see what you mean.

littledan commented 6 years ago

I like @claudepache 's suggestion in https://github.com/tc39/proposal-optional-chaining/issues/55#issuecomment-372451816 . This proposal specifically tries to be relatively minimal in the constructs supported, compared to the CoffeeScript equivalent, to keep things easy to understand.

claudepache commented 6 years ago

Besides spread, there are other constructs that expect iterable and may be made optional:

claudepache commented 6 years ago

Regret: for (let x of foo) and yield* foo should have been:

so that optional spread would have worked everywhere.

js-choi commented 6 years ago

Heh—for (let x of? foo) and yield *? foo, someday, perhaps?