【发布时间】:2013-02-08 14:27:03
【问题描述】:
我是新手 groovy 程序员,我遇到了使用静态编译 (@CompileStaticannotation) 的 switch-case-break 语句的奇怪行为。 breaks 似乎被忽略了。
是错误还是我在阅读文档时遗漏了什么。
环境:
- groovy sdk 2.1.0
- Oracle JDK build 1.7.0_13-b20 on Mac OS X Lion 10.7.5
测试用例:
import groovy.transform.CompileStatic
@CompileStatic
class Test {
def test() {
['A', 'B', 'C'].each { String val ->
switch (val) {
case 'A' :
println("${val} caseA")
break
case 'B' :
println("${val} caseB")
break
default :
println("${val} default")
}
}
}
}
(new Test()).test()
输出:
A caseA
A caseB
A default
B caseB
B default
C default
第二次测试:评论@CompileStatic
一切正常:
A caseA
B caseB
C default
【问题讨论】:
-
在我看来是个 bug,你能在 the Groovy JIRA 上报告它吗?
-
是的,我刚刚举报了GROOVY-5984
-
酷 :-) (looks like it was fixed yesterday) 我在下面发布了一个模糊的不愉快的解决方法,以使其与 2.1.0 一起使用 :-)
标签: groovy switch-statement compile-static