Closed justinfagnani closed 5 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.)
Also, with the nullish coalescing operator, I think foo(...args ?? [])
and [...(listOne ?? [])]
would precisely match the semantics you'd want ...?
to have?
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.
You could use an empty string as well, but i see what you mean.
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.
Besides spread, there are other constructs that expect iterable and may be made optional:
[ ?...iterableOrNull ]
, f(?...iterableOrNull)
for (let x ?of iterableOrNull)
?yield* iterableOrNull
Regret: for (let x of foo)
and yield* foo
should have been:
for (let x = ...foo)
yield ...foo
so that optional spread would have worked everywhere.
Heh—for (let x of? foo)
and yield *? foo
, someday, perhaps?
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:
Instead of: