在 Haxe switch/case 中,有没有办法将 case 'xx': 与默认值:?

Avt*_*t'W 2 haxe

我喜欢使用switch/case很多,但我一直想知道是否可以将默认值与 case 混合使用Haxe(版本 2 或 3)?

这是我试图实现的一个例子,除了它不编译:

switch(s) {
    case 'reset': trace("...");
    case 'stat','stats': trace("...");        // This compiles ok in case some people don't know
    case 'help', default: trace("...");       // This doesn't compile
}
Run Code Online (Sandbox Code Playgroud)

你知道这是否可能吗?有时我更喜欢这种方式,因为即使它相当于只使用默认值,但有时这种方式对读者来说代码更精确(没有歧义或混淆)。

Avt*_*t'W 5

我刚刚设法为Haxe 3找出了一个可行的解决方案。不幸的是,它不能在 Haxe 2.10(我现在使用的版本)中编译。(编辑:我找到了 Haxe 2 的方法,请参阅下面的其他答案)。

此处的文档(http://haxe.org/manual/lf-pattern-matching-introduction.html)说“ _ 模式匹配任何内容,因此 case _: 等于 default: ”。

与写作类似,case 'stat', 'stats':我尝试编写case 'help', _:并编译。

所以 Haxe 3 的缩写是:

switch(s) {
    case 'reset': trace("...");
    case 'stat','stats': trace("...");
    case 'help', _: trace("...");
}
Run Code Online (Sandbox Code Playgroud)

我在这里编译了一个例子:http : //try.haxe.org/#A8D15