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

Slice literal assignment #13

Closed
liamoc opened this issue Dec 28, 2009 · 3 comments
Closed

Slice literal assignment #13

liamoc opened this issue Dec 28, 2009 · 3 comments

Comments

@liamoc
Copy link

liamoc commented Dec 28, 2009

The following code

a[5..8] = [1,2,3]

Compiles to

(function(){
  a.slice(5, 8 + 1) = [1, 2, 3];
})();

I suggest it should compile to:

(function(){
    a.splice.apply(a,[1,3].concat([1,2,3]));
    return a;
})();          

The main problem with this approach is simply that the return value of that expression is what is removed not the new array, which would fit better with assignment semantics, hence the return statement.

@jashkenas
Copy link
Owner

Very nice idea. Splice literal to go with the slice literal. It can't be a "return", but I think we can do it with the JS comma operator.

(a.splice.apply(a,[1,3].concat([1,2,3])), a)

@liamoc
Copy link
Author

liamoc commented Jan 1, 2010

Sounds good.

@jashkenas
Copy link
Owner

Splice literals are now on master. Your first example:

a[5..8] = [1,2,3]

Compiles to:

a.splice.apply(a, [5, 8 - 5 + 1].concat([1, 2, 3]));

You can use inclusive or exclusive ranges, as usual. We actually don't need to worry about the return value, because splices modify the array in place, and it's an AssignNode that's doing the compilation, suppressing the actual, y'know, assignment.

Closing the ticket.

stephank pushed a commit to stephank/coffee-script that referenced this issue May 14, 2011
katlogic referenced this issue in katlogic/darkscript Oct 6, 2014
…on-top-ast-node

Set file number on top AST node
alangpierce pushed a commit to alangpierce/coffeescript that referenced this issue Jan 2, 2017
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants