我今天开始学习 Dart,我遇到了一些我的谷歌技能无法找到的东西。
我如何在非空案例中失败?
我的用例是这样的:我正在编写一个 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());
}
现在,第二个选项看起来不错,但是您必须记住有 11 个案例,这意味着有 11 个
if (type in [])
,这是我想要的更多打字。那么, Dart 有没有
// //$FALL-THROUGH$
我不知道的?谢谢。
最佳答案
Dart 规范提供了一种使用 "continue"使 switch case 继续到另一个 switch case 的方法:
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-statement - 在 Dart 中切换失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12550164/