Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Array.from instead of [].slice #963

Closed
KSXGitHub opened this issue May 6, 2017 · 5 comments · Fixed by #965
Closed

Use Array.from instead of [].slice #963

KSXGitHub opened this issue May 6, 2017 · 5 comments · Fixed by #965
Assignees
Labels

Comments

@KSXGitHub
Copy link

KSXGitHub commented May 6, 2017

Example code

function * gen ()
  yield 123
  yield 456
  yield 789

console.log [...gen()]

Expected JavaScript

function* gen(){
  (yield 123);
  (yield 456);
  return (yield 789);
}
console.log(Array.from(gen())); // This would print [123, 456, 789]

Current JavaScript

var slice$ = [].slice;
function* gen(){
  (yield 123);
  (yield 456);
  return (yield 789);
}
console.log(slice$.call(gen())); // This would print an empty array
@rhendric rhendric self-assigned this May 6, 2017
@rhendric rhendric added the bug label May 6, 2017
@dk00
Copy link
Contributor

dk00 commented May 6, 2017

Array.from requires ES6 support, and if a JS engine knows Array.from, it should also know spread(...expression)

If we decide to output ES6, we can use spread here, spread can be used with any iterables(including generators) like Array.from.

splat-in-array = [a, ...b, c, ...gen!]
splat-in-object = {a, ...b, c, ...d}
call-with-splat-parameters = fn a, ...b, c, ...d
var splatInArray, splatInObject, callWithSplatParameters
splatInArray = [a, ...b, c, ...gen()];
splatInObject = {
  a,
  ...b,
  c,
  ...d
};
callWithSplatParameters = fn(a, ...b, c, ...d);

@rhendric
Copy link
Collaborator

rhendric commented May 7, 2017

Targeting ES6 has been discussed elsewhere, I believe, and if it's worth doing, it's worth doing in the context of a broader review of the backend, such as #862. In the meantime, this can certainly be addressed in an ES3-compatible way, which is my intent.

@KSXGitHub
Copy link
Author

@dk00 We can use polyfill

rhendric added a commit to rhendric/LiveScript that referenced this issue May 15, 2017
Prior to this commit, [].slice was used to turn array-like things into
arrays when needed for splats in array literals. This commit uses
Array.from instead, when available at runtime, and falls back to slice
when not. This allows a wider range of iterables to be used as splats,
on the platforms that support them (platforms missing Array.from will
also be missing more exotic iterables).

Fix gkz#963.
@vendethiel
Copy link
Contributor

@rhendric congrats on the badge!

rhendric added a commit that referenced this issue May 22, 2017
Prior to this commit, [].slice was used to turn array-like things into
arrays when needed for splats in array literals. This commit uses
Array.from instead, when available at runtime, and falls back to slice
when not. This allows a wider range of iterables to be used as splats,
on the platforms that support them (platforms missing Array.from will
also be missing more exotic iterables).

Fix #963.
@determin1st
Copy link

thanks 8)

This was referenced Jan 11, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants