Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch fallthrough in Dart

I started learning Dart today, and I've come across something that my google skills are having trouble finding.

How do I have a fall-through in a non-empty case?

My use case is this: I'm writing a sprintf implementation (since dart doesn't have this too), which would work except for this fall-through thing. When parsing the variable type you can, for example, have "%x" versus "%X" where the upper case type tells the formatter that the output is supposed to be uppercase.

The semi-pseudocode looks like:

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

The other ways I can think of doing this, would one of the following

1:

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

2:

var type = getType();
if (type in ['x', 'X']) {
   return formatHex('X' == getType());
}

Now, the second choice almost looks good, but then you have to remember that there are eleven cases, which would mean having eleven if (type in []), which is more typing that I'd like.

So, does dart have some // //$FALL-THROUGH$ that I don't know about?

Thanks.

like image 955
Naddiseo Avatar asked Sep 23 '12 06:09

Naddiseo


People also ask

What is Fallthrough switch statement?

Code Inspection: Fallthrough in 'switch' statementReports a switch statement where control can proceed from a branch to the next one. Such "fall-through" often indicates an error, for example, a missing break or return .

How do you return a switch case in darts?

Most commonly, you'll use break or return . break simply exits out of the switch; it doesn't have any other effect. It doesn't return a value. In Dart, a return statement immediately ends the function's execution, and therefore it will break out of a switch statement.

What is Fallthrough in switch statement in C#?

Fall through is a type of error that occurs in various programming languages like C, C++, Java, Dart …etc. It occurs in switch-case statements where when we forget to add break statement and in that case flow of control jumps to the next line. until the break is reached or end of the switch is reached.”


2 Answers

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.

like image 140
Lasse Nielsen Avatar answered Sep 22 '22 08:09

Lasse Nielsen


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;
}
like image 37
adam-singer Avatar answered Sep 25 '22 08:09

adam-singer