@类型化注释停止Groovy代码编译
为什么这个Groovy代码@类型化注释停止Groovy代码编译,groovy,groovy++,Groovy,Groovy++,为什么这个Groovy代码 def mt(){ def i= 0 def c= {i++} } @Typed def mt(){ def i= 0 def c= {i++} } …编译,但是这个Groovy代码 def mt(){ def i= 0 def c= {i++} } @Typed def mt(){ def i= 0 def c= {i++} } …未编译时出错 org.codehaus.groovy.control.MultipleCompi
def mt(){
def i= 0
def c= {i++}
}
@Typed def mt(){
def i= 0
def c= {i++}
}
…编译,但是这个Groovy代码
def mt(){
def i= 0
def c= {i++}
}
@Typed def mt(){
def i= 0
def c= {i++}
}
…未编译时出错
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
C:\Users\gavin\Documents\Personal\Groovy\otherRun.groovy: 5:
Cannot modify final field otherRun$mt$1.i @ line 5, column 11.
def c= {i++}
^
发布到谷歌代码跟踪器的声明:
这是故意的
以及指向a的链接,其中说明:
是的,这是与标准Groovy最显著的区别之一。
在Groovy++中,共享闭包变量总是最终的
我看不出如何以groovypp友好的方式重写现有代码,因此我猜您要么需要重构代码以另一种方式完成,要么不将其声明为@Typed
编辑:我想你可以将行为封装在一个类中,并返回一个成员函数的方法句柄发布到google代码跟踪器的状态:
这是故意的
以及指向a的链接,其中说明:
是的,这是与标准Groovy最显著的区别之一。
在Groovy++中,共享闭包变量总是最终的
我看不出如何以groovypp友好的方式重写现有代码,因此我猜您要么需要重构代码以另一种方式完成,要么不将其声明为@Typed
编辑:我想您可以将行为封装在类中,并将方法句柄返回给成员函数您可以通过@Field注释绕过限制,如下所示:
@Typed def mt(){
@Field def i = 0
def c = {i++}
}
assert mt().call() == 0
assert mt().call() == 1
您可以通过@Field注释绕过该限制,如下所示:
@Typed def mt(){
@Field def i = 0
def c = {i++}
}
assert mt().call() == 0
assert mt().call() == 1
刚刚意识到我可以使用一个引用
@Typed def mt(){defi=new reference(0)def c={I.set(I.get()+1)}
当然更详细。刚刚意识到我可以使用一个引用@Typed def mt(){defi=new reference(0)def c={I.set(I.get()+1)}
当然更详细。