Switch to Dart

I started learning Dart today, and I was faced with the fact that I had no problems finding google.

How do I go about failure in a non-empty case?

My usage example: I am writing an implementation of sprintf (since the dart also does not have this), which will work, except for this failed thing. When analyzing the type of a variable, you can, for example, have "% x" compared to "% X", where the uppercase type tells the formatting that the output should be uppercase.

The semi-pseudo-code is as follows:

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

Other ways I can think of would be 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 option looks good, but then you have to remember that there are eleven cases, which would mean eleven if (type in []), which is more typing the text that I would like.

, dart // //$FALL-THROUGH$, ?

.

+5
4

Dart , "":

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

VM, , , dart2js .

+4

case Dart, , .

, - , switch, , , switch.

, - :

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

, . , case case, switch.

, , . , - case. :

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

( C), , , .

+3

dart language tour, (2) .

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

, -

var command = 'OPEN';
switch (command) {

  case 'OPEN':
    executeOpen();
    // ERROR: Missing break causes an exception to be thrown!!

  case 'CLOSED':
    executeClose();
    break;
}
+1

, continue; continue <label>;, :

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

All Articles