gkz / LiveScript

LiveScript is a language which compiles to JavaScript. It has a straightforward mapping to JavaScript and allows you to write expressive code devoid of repetitive boilerplate. While LiveScript adds many features to assist in functional style programming, it also has many improvements for object oriented and imperative programming.
http://livescript.net
MIT License
2.31k stars 156 forks source link

Return list in a function #1089

Closed anhnhoktvn closed 4 years ago

anhnhoktvn commented 4 years ago

I expect

data: -> 
  * name: 'item 1'
    rating: 4.8
  * name: 'item 2'
    rating: 4.9

will be converted to

({
  data: function(){
    return [
      {
        name: 'item 1',
        rating: 4.8
      }, {
        name: 'item 2',
        rating: 4.9
      }
    ];
  }
});

but it is converted to:

({
  data: function(){
    ({
      name: 'item 1',
      rating: 4.8
    });
    return {
      name: 'item 2',
      rating: 4.9
    };
  }
});
rhendric commented 4 years ago

Ah, the oft-misunderstood implicit structure asterisk. Though it seems many users disagree with this design decision, it is in fact functioning as documented. The key thing to understand is that * does not create a list; it only separates the two objects from each other. In some contexts like the right-hand side of an assignment, two expressions next to each other at the same level of indentation are implicitly converted to a list. But for obvious reasons, the body of a function is not such a context. See https://github.com/gkz/LiveScript/issues/1055#issuecomment-402785740 for a workaround and links to other duplicate reports of this ‘issue’.