coffeescript
coffeescript copied to clipboard
Nodes: `while` with empty block is inconsistent with empty-block `for`
-
forwith single-expression block containingundefined:$ coffee -be 'console.log (for a in [0, 1, 2, 3] then undefined)' [ undefined, undefined, undefined, undefined ] -
forwith empty block:$ coffee -be 'console.log (for a in [0, 1, 2, 3] then)' [] -
whilewith single-expression block containingundefined:$ coffee -be 'i = 4; console.log (while i-- then undefined)' [ undefined, undefined, undefined, undefined ] -
whilewith 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.
Why should bodiless loops be allowed at all? Is there a use case?
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?
To me it just kind of looks like starting a sentence and then not
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.
That's great. That's two special cases I can remove from my compiler.