为什么这段Groovy代码...
def mt(){
def i= 0
def c= {i++}
}但是这段...compile代码..。
@Typed def mt(){
def i= 0
def c= {i++}
}...not编译出错...
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++}
^发布于 2011-08-03 03:22:05
您可以通过@Field注释绕过该限制,如下所示:
@Typed def mt(){
@Field def i = 0
def c = {i++}
}
assert mt().call() == 0
assert mt().call() == 1发布于 2011-08-02 21:39:39
This issue在谷歌代码跟踪器上写道:
这是设计好的。
以及指向message on the user group的链接,该And声明:
是的,这是与标准Groovy最重要的区别之一。在Groovy++中,共享闭包变量总是最终变量。
我不明白如何以groovypp友好的方式重写代码,所以我猜您要么需要重构代码以实现另一种方式,要么不将其声明为@Typed
编辑:我想您可以将行为封装在一个类中,然后将方法句柄返回给一个成员函数
https://stackoverflow.com/questions/6912769
复制相似问题