You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The text was updated successfully, but these errors were encountered:
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.
Uh oh!
There was an error while loading. Please reload this page.
The idea is to modernize
break
statement in every construct where it's currently defined by adding a new parametervalue
. The general form of break statement becomes:break label->value;
In this extended form, the keywordbreak
can be omitted (it becomes redundant when->
is present), so we can write it aslabel->value
, which further degenerates into->value
if the label is omitted.Now every construct that currently supports
break
might be used as an expression:It would probably make sense to also add support for "when" block (as a statement and as an expression):
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 supportsbreak
only in a formbreak 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 standardreturn
statement.The text was updated successfully, but these errors were encountered: