我今天开始学习Dart,我发现我的谷歌技能很难找到一些东西.

我怎样才能在非空 case 中失败呢?

我的用例是这样的:我正在编写一个sprintf实现(因为dart也没有这个),除了这个失败的东西之外,它还能工作.例如,在分析变量类型时,可以使用"%x"和"%x",其中大写类型告诉格式化程序输出应该是大写的.

半伪代码如下所示:

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

我能想到的其他方法是:

1:

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

2:

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

现在,第二个 Select 看起来几乎不错,但是你要记住有11个 case ,这意味着有11if (type in [])个 case ,这是我想要的更多的打字.

那么,达特有没有// //$FALL-THROUGH$来个我不知道的?

谢谢你.

推荐答案

Dart规范提供了一种方法,使一个switch 箱可以使用"continue"继续切换到另一个switch 箱:

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 switch实现还不支持该功能.

Dart相关问答推荐

Flutter 是否提供即时应用程序?

如何在 Dart 单元测试中模拟或验证打印调用?

如何为 spawnUri 动态构建 Dart 脚本?

Dart 捕获 http 异常

如何计算列表中元素的出现次数

为什么使用Dart作为前端开发人员?

Flutter-创建一个倒计时(countdown)小部件

如何在 Dart 语言中将 Future 转换为 List

找不到正确的提供程序-Flutter

如何在flatter中读取本地json导入?

将ListTile图标向左对齐

如何左对齐 Flutter 中的 OutlineButton 图标

将符号转换为字符串

dart 中隐式转换运算符的语法是什么?

Dart 中 Promise.all 的类比?

具有列表的 Null 感知运算符

将`_`(即下划线)作为唯一参数传递给 Dart 语言函数是什么意思?

判断dart中的字符串是否为数字

何时在 Dart 中使用 part/part of 与 import/export?

dart列表最小值/最大值