Skip to content

Proposal: adding value to 'break' statement #762

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

Closed
ghost opened this issue Dec 22, 2019 · 2 comments
Closed

Proposal: adding value to 'break' statement #762

ghost opened this issue Dec 22, 2019 · 2 comments

Comments

@ghost
Copy link

ghost commented Dec 22, 2019

The idea is to modernize break statement in every construct where it's currently defined by adding a new parameter value. The general form of break statement becomes: break label->value; In this extended form, the keyword break can be omitted (it becomes redundant when -> is present), so we can write it as label->value, which further degenerates into ->value if the label is omitted.

Now every construct that currently supports break might be used as an expression:

final String numericString =
   switch (integer) {  
      case 1 : -> "uno";
      case 2 : print("debug print"); ->"dos";
      case 3 : -> "tres";
      default :-> "N/A";
   };
var x = for (int i=0; i<10; i++) {  
   if (someCondition(i)) -> i;
} 
if (x!=null) { // if the search is not successful, x is null
   // do something
}
// same for "while" block

It would probably make sense to also add support for "when" block (as a statement and as an expression):

var x = ...;
var y = when {
   case x <= 5: -> "a";
   case x > 5 && x < 10: -> "b"; 
   case x >= 10 && x < 20: -> "c"
   default: ->"d";
}

NOTE: the natural idea would be to extend the proposed syntax to block statement {...} thus turning it into block-expression, but it doesn't quite work out: block supports break only in a form break label. However, the following syntax is not valid in dart:
var x=label: {statements... break label; ...}

Anyway, block-expressions won't add too much power to the language: if anonymous methods (#260) are implemented, then we can always find a suitable receiver to achieve the same effect via anonymous method call obj.{...} that returns the result using a standard return statement.

@ghost ghost mentioned this issue Dec 22, 2019
@ghost ghost changed the title Proposal: adding value to break statement Proposal: adding value to 'break' statement Dec 23, 2019
@ghost ghost mentioned this issue Dec 27, 2019
@lrhn
Copy link
Member

lrhn commented Jan 6, 2020

This would fit better into a setting where all statements have a value and can be used as expressions.
Then var x = label: { for (var something in somewhat) if (test) label -> value;}; could work.

We are a little hampered by labels not being in the same namespace as everything else. Otherwise we could just use break label value with both label and value being optional.

@ghost ghost closed this as completed Jan 24, 2022
@nioncode
Copy link

@tatumizer why did you close this? Was this introduced into dart?

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants