Switch fallthrough in Dart

ID : 274538

viewed : 31

Tags : switch-statementdartfall-throughswitch-statement





Top 5 Answer for Switch fallthrough in Dart

vote vote

91

The Dart specification gives a way for a switch case to continue to another switch case using "continue":

switch (x) {   case 42: print("hello");            continue world;   case 37: print("goodbye");            break;   world:  // This is a label on the switch case.   case 87: print("world"); } 

It works in the VM, but sadly the dart2js switch implementation doesn't yet support that feature.

vote vote

84

From the dart language tour, your example of (2) should be correct.

var command = 'CLOSED'; switch (command) {   case 'CLOSED':     // Empty case falls through.   case 'NOW_CLOSED':     // Runs for both CLOSED and NOW_CLOSED.     executeClose();     break; } 

It would be an error if you tried to do something as follows

var command = 'OPEN'; switch (command) {    case 'OPEN':     executeOpen();     // ERROR: Missing break causes an exception to be thrown!!    case 'CLOSED':     executeClose();     break; } 
vote vote

73

EDIT: This seems to only have worked due to a bug in the implementation of Dart. See the Dart 1.x language spec, section "17.15 Continue".

The bug is now fixed, as reported in a comment, so this no longer works. Keeping for historical reasons.


Also, a simplification of Lasse's answer is to use a simple continue; instead of continue <label>; as in:

bool is_upper = false; switch (getType()) {     case 'X':       is_upper = true;       continue;     case 'x':       return formatHex(is_upper); } 
vote vote

70

You can't have a non-empty case body in Dart that falls through, this will raise an error.

What I tend to do with anything other than very simple switch statements is to refactor all the common code out into functions, so that you don't have this multi-level control flow in the switch itself.

In other words, something like:

switch (getType()) {     case 'X':         return formatHex(true);     case 'x':         return formatHex(false); } 

There's no reason why you need to have fallthrough. It comes in handy when the actions in a case section can be carried out in toto at the end of another case section, but this method can do that without fallthrough and without making your switch statement complex.

It can also handle more complex cases where there are common actions that aren't included in toto at the end. For example, you may want to do something at the start or in the middle of the case section. Calling common functions handles that more than well enough:

switch (getType()) {     case 'X':         doSomethingOnlyForUpperCase();         doSomethingCommon();         doSomethingElseOnlyForUpperCase();         return formatHex(true);     case 'x':         doSomethingCommon();         return formatHex(false); } 

I actually also do this for languages (such as C) that support this sort of non-empty fall-through since I believe it aids in readability and maintainability.

vote vote

53

Dart in 202207, just empty, instead of continue label

PermissionStatus recordStatus =               await Permission.requestSinglePermission(                   PermissionName.Microphone);  switch (recordStatus) {       case PermissionStatus.allow:       case PermissionStatus.always:       case PermissionStatus.whenInUse:           return;       default:           break;  } 

Top 3 video Explaining Switch fallthrough in Dart







Related QUESTION?