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.32k stars 155 forks source link

How to make a function that returns an object #1055

Closed danielo515 closed 6 years ago

danielo515 commented 6 years ago

Hello, I want to build a function that returns an array of objects. The array is quite large, so I want it to define vertically. However, the compiler does weird things, the only workaround I found is this:

makeQuestions = ({dependencies, devDependencies}) ->
    questions =
        * type: \input
          name: \pkg.name
          message: 'Name the package'
          filter: (.split ' ' .join \- .toLowerCase!)
        * type: \confirm
          name: \meta.babel
          message: 'Is this a babel plugin ?'
          default: false

this returns the assignment, but I find ridiculous to create an unused variable

vendethiel commented 6 years ago

Yeah, that one's a bit annoying. https://github.com/gkz/LiveScript/issues/1000#issuecomment-358728417

-> [] =
  * a: 1
  * a: 2
determin1st commented 6 years ago

curly brackets improve (imo) visual look of the blocks:

makeQuestions = ({dependencies, devDependencies}) ->
    return
        {
            type: \input
            name: \pkg.name
            message: 'Name the package'
            filter: (.split ' ' .join \- .toLowerCase!)
        }
        {
            type: \confirm
            name: \meta.babel
            message: 'Is this a babel plugin ?'
            default: false
        }
        ...
danielo515 commented 6 years ago

Leaving curly braces apart is one of the reasons why I switched to livescript

rhendric commented 6 years ago

The gkz-recommended way to do this (#421) is:

makeQuestions = ({dependencies, devDependencies}) ->
    *   * type: \input
          name: \pkg.name
          message: 'Name the package'
          filter: (.split ' ' .join \- .toLowerCase!)
        * type: \confirm
          name: \meta.babel
          message: 'Is this a babel plugin ?'
          default: false

See also #512, #784 for some more discussion. I'm going to close this as a duplicate; I think those issues and #1000 already cover that there is some awkwardness around this syntax.