Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign upArray spread throws on undefined/null but object spread doesn't #687
Comments
olalonde
changed the title from
Array spread undefined/null
to
Array spread throws on undefined/null but object spread doesn't
Sep 11, 2016
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
ljharb
Sep 11, 2016
Member
The right place would be the repo for the object spread proposal - the rationale is because it 1:1 maps to Object.assign.
|
The right place would be the repo for the object spread proposal - the rationale is because it 1:1 maps to |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
olalonde
Sep 11, 2016
Yeah, I prefer that behaviour actually. I was more hoping that array spread could adopt the same behaviour but I suppose that ship has sailed?
olalonde
commented
Sep 11, 2016
•
|
Yeah, I prefer that behaviour actually. I was more hoping that array spread could adopt the same behaviour but I suppose that ship has sailed? |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
ljharb
Sep 11, 2016
Member
Array spread isn't "array spread", it's "iterable spread", so different rules apply.
|
Array spread isn't "array spread", it's "iterable spread", so different rules apply. |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
olalonde
Sep 11, 2016
Would a proposal to modify "iterable spread" to ignore undefined/null be considered?
olalonde
commented
Sep 11, 2016
|
Would a proposal to modify "iterable spread" to ignore undefined/null be considered? |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
ljharb
Sep 11, 2016
Member
That would be a breaking change for anybody who was relying on the throw behavior. What would be the benefit of that, when you can do ...(maybeArr || []) if that's the behavior you want?
|
That would be a breaking change for anybody who was relying on the throw behavior. What would be the benefit of that, when you can do |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
olalonde
Sep 11, 2016
Yes, it would indeed be breaking which is why I supposed the ship had sailed. Does Ecmascript ever break forward compatibility? ...(maybeArr || []) is what I've been doing in my own code, or with default params when possible (arr = []) => ([...arr]). I do think it would've been more elegant for [...arr] and {...obj} to treat undefined/null similarly. Going to close this because I don't think there'd be a way to change that behaviour without breaking forward compatibility.
olalonde
commented
Sep 11, 2016
|
Yes, it would indeed be breaking which is why I supposed the ship had sailed. Does Ecmascript ever break forward compatibility? |
olalonde
closed this
Sep 11, 2016
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
mcmar
Jul 20, 2017
This little "gotcha" just caught me by surprise too. I can understand the history of how this happened, but it does look like a mistake in language design from my perspective. Maintaining equivalency between the two spread operator syntaxes helps developers. Javascript already has too many gotchas. I'd hope we could avoid introducing more.
And while I understand the need to maintain compatibility, I also doubt that there are many people who are relying on the spread operator throwing an error for their code to function correctly.
mcmar
commented
Jul 20, 2017
|
This little "gotcha" just caught me by surprise too. I can understand the history of how this happened, but it does look like a mistake in language design from my perspective. Maintaining equivalency between the two spread operator syntaxes helps developers. Javascript already has too many gotchas. I'd hope we could avoid introducing more. |
olalonde commentedSep 11, 2016
•
edited
(Pardon if this isn't the right place to discuss this)
I've noticed an annoying inconsistency between how object and array spread behave, namely that object spread silently ignores
null/undefinedwhile array spread throws an error:I was wondering what was the rationale for array spread throwing an error on
undefined/nulland if perhaps object spread could adopt the same behaviour or vice versa.