Switch fallthrough in Dart Switch fallthrough in Dart dart dart

Switch fallthrough in Dart


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.


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;}


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);}