Skip to content
This repository has been archived by the owner on Jan 25, 2022. It is now read-only.

Optional spread syntax #55

Closed
justinfagnani opened this issue Mar 12, 2018 · 8 comments
Closed

Optional spread syntax #55

justinfagnani opened this issue Mar 12, 2018 · 8 comments

Comments

@justinfagnani
Copy link

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
Copy link
Collaborator

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
Copy link
Member

ljharb commented Mar 12, 2018

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

@justinfagnani
Copy link
Author

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
Copy link
Member

ljharb commented Mar 13, 2018

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

@littledan
Copy link
Member

I like @claudepache 's suggestion in #55 (comment) . This proposal specifically tries to be relatively minimal in the constructs supported, compared to the CoffeeScript equivalent, to keep things easy to understand.

@claudepache
Copy link
Collaborator

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

  • optional spread: [ ?...iterableOrNull ], f(?...iterableOrNull)
  • optional loop: for (let x ?of iterableOrNull)
  • optional delegation: ?yield* iterableOrNull

@claudepache
Copy link
Collaborator

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.

@js-choi
Copy link

js-choi commented Mar 14, 2018

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants