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

Open
michaelficarra opened this Issue Jul 17, 2012 · 5 comments

Comments

Projects
None yet
4 participants
Collaborator

michaelficarra commented Jul 17, 2012

  • 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.

erisdev commented Jul 17, 2012

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

Collaborator

michaelficarra commented Jul 17, 2012

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?

erisdev commented Jul 18, 2012

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

Owner

jashkenas commented Jul 18, 2012

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.

Collaborator

michaelficarra commented Jul 18, 2012

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

@GeoffreyBooth GeoffreyBooth changed the title from `while` with empty block is inconsistent with empty-block `for` to Nodes: `while` with empty block is inconsistent with empty-block `for` May 5, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment