Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "else" clause to loop constructs. #21481

Closed
lrhn opened this issue Oct 31, 2014 · 1 comment
Closed

Add "else" clause to loop constructs. #21481

lrhn opened this issue Oct 31, 2014 · 1 comment
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-duplicate Closed in favor of an existing report type-enhancement A request for a change that isn't a bug

Comments

@lrhn
Copy link
Member

lrhn commented Oct 31, 2014

Please add an "else" clause to loop constructs.
The else clause would be written as:

  for/while(...) { 
    body 
  } else {
    else-part
  }

It will only be executed when the loop condition fails. An unlabeled break inside the body will break past the else-part. Continues inside the else part can not continue the loop, unlabeled breaks in the else part do not refer to the loop.

This feature would make some algorithms much easier to expression, and avoid the labeled breaks that is often used instead.

Example code:

    for (int i = 0; i < string.length; i++) {
      if (string.codeUnitAt(i) >= 0x80) break;
    } else {
      return string.codeUnits;
    }
    return UTF8.encode(string);

What I currently have to write:

    checkAscii: {
      for (int i = 0; i < string.length; i++) {
        if (string.codeUnitAt(i) >= 0x80) break checkAscii;
      }
      return string.codeUnits;
    }
    return UTF8.encode(string);

This also shows a valid rewrite to, e.g., JavaScript.

I'm only aware of one language that currently has this feature: Python.

@lrhn lrhn added Type-Enhancement area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). labels Oct 31, 2014
@kevmoo kevmoo added type-enhancement A request for a change that isn't a bug and removed triaged labels Mar 1, 2016
@lrhn
Copy link
Member Author

lrhn commented Sep 14, 2020

Refer to dart-lang/language#171 in the language-repo instead.

@lrhn lrhn closed this as completed Sep 14, 2020
@lrhn lrhn added the closed-duplicate Closed in favor of an existing report label Sep 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-duplicate Closed in favor of an existing report type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

2 participants