coffeescript icon indicating copy to clipboard operation
coffeescript copied to clipboard

Nodes: `while` with empty block is inconsistent with empty-block `for`

Open michaelficarra opened this issue 13 years ago • 5 comments

  • for with single-expression block containing undefined:

    $ coffee -be 'console.log (for a in [0, 1, 2, 3] then undefined)'
    [ undefined, undefined, undefined, undefined ]
    
  • for with empty block:

    $ coffee -be 'console.log (for a in [0, 1, 2, 3] then)'
    []  
    
  • while with single-expression block containing undefined:

    $ coffee -be 'i = 4; console.log (while i-- then undefined)'
    [ undefined, undefined, undefined, undefined ]
    
  • while with empty block:

    $ coffee -be 'i = 4; console.log (while i-- then)'
    undefined
    

The value of a for or while should always be a list. The behaviour of while with an empty block should change to produce an empty list so that it is consistent with for.

edit: an alternative (which I would actually prefer) would be to change the behaviour of the empty-block forms to match the forms with an undefined expression.

michaelficarra avatar Jul 17 '12 22:07 michaelficarra

Why should bodiless loops be allowed at all? Is there a use case?

erisdev avatar Jul 17 '12 23:07 erisdev

Instinctually, they imply a then undefined to me, which is why I would prefer the alternative solution I mentioned. It's also more consistent with (if condition then), which does imply undefined because what else would the value of a single empty block be?

michaelficarra avatar Jul 17 '12 23:07 michaelficarra

To me it just kind of looks like starting a sentence and then not

erisdev avatar Jul 18 '12 03:07 erisdev

They're certainly not something that you would use in real code, but they might occur from time to time as you comment out the body of a loop. I remember that being part of the original rationale for parsing them in the first place.

@michaelficarra -- sounds good. I'd agree that the value of looping over an empty body is undefined for each iteration through the loop.

jashkenas avatar Jul 18 '12 14:07 jashkenas

That's great. That's two special cases I can remove from my compiler.

michaelficarra avatar Jul 18 '12 14:07 michaelficarra