@Typed 注释停止 Groovy 代码编译
为什么这个 Groovy 代码...
def mt(){
def i= 0
def c= {i++}
}
...可以编译,但是这个 Groovy 代码...
@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++}
^
Why does this Groovy code...
def mt(){
def i= 0
def c= {i++}
}
...compile, but this Groovy code...
@Typed def mt(){
def i= 0
def c= {i++}
}
...not compile with error...
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++}
^
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过 @Field 注释来解决该限制,如下所示:
You can work around the restriction via the @Field annotation, like so:
发布到 Google Code Tracker 的此问题指出:
以及有关用户组的消息的链接,其中指出:
我看不出你如何以 groovypp 友好的方式重写代码,所以我猜你要么需要重构代码以另一种方式来做,要么不将其声明为 @Typed
Edit< /strong>:我想你可以将行为封装在一个类中,并将方法句柄返回给成员函数
This issue posted to the google code tracker states:
And links to a message on the user group, which states:
I cannot see how you could rewrite that code you have in a groovypp friendly way, so I guess you would either need to refactor the code to do it another way, or else not declare it as @Typed
Edit: I guess you could encapsulate the behaviour in a class, and return a method handle to a member function