proposal-do-expressions: Clarify interaction between do-expressions and break

What does break; do inside a do-expression inside a loop? @ljharb and I had opposite intuitions, both of which seem reasonable (to me). (See issue #22.)

The proposal says,

Tennant’s Correspondence Principle

[…]

  • (do { <stmt> };) equivalent to { <stmt> }

I take this to mean that (do { break; }); is equivalent to break; and therefore do-expressions are invisible to break. (To me, the use of the phrase “Tennant’s Correspondence Principle” indicates that this is intentional; maybe @dherman can confirm or deny?)

About this issue

  • Original URL
  • State: open
  • Created 6 years ago
  • Reactions: 1
  • Comments: 18 (9 by maintainers)

Most upvoted comments

I read the whole thread and agree (do { break; }); should equals break;. . But I also suspect maybe we should make do { break; } syntax error just like current eval('break;') if there is no strong use cases.

The only near-real use case in this thread is

while (getThing()) {
  items.push(do {
    const value = getItem();
    if (!value) continue;
    value;
  });
}

But I feel this example just show the dark side of allowing direct continue/break in the do block. It’s not easy to recognize there is a continue statement inside and actually dismiss the push operation. As a code reviewer, I would prefer the plain code like

while (getThing()) {
  const value = getItem();
  if (value) items.push(value);
}

Much simpler, shorter and clearer.

Hope someone can give some good use cases.