加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

如何在Groovy中定义@CompileStatic兼容的闭包?

发布时间:2020-12-14 16:23:04 所属栏目:大数据 来源:网络整理
导读:我有一个类,其闭包定义为: void everyPixel( closure ){ for( def x : 0..width-1 ) for( def y : 0..height-1 ) closure( x,y )} 但是如果我将@CompileStatic注释应用于它,它将不会编译(它在我添加闭包之前做了),并带有以下消息: Groovyc: [Static type c
我有一个类,其闭包定义为:

void everyPixel( closure ){
    for( def x : 0..width-1 )
        for( def y : 0..height-1 )
            closure( x,y )
}

但是如果我将@CompileStatic注释应用于它,它将不会编译(它在我添加闭包之前做了),并带有以下消息:

Groovyc: [Static type checking] – Cannot find matching method java.lang.Object#call(java.lang.Integer,java.lang.Integer). Please check if the declared type is right and if the method exists.

如何为此创建类型签名以便静态编译?到目前为止,我在Google上的所有点击都说明了如何传递闭包,而不是如何定义接受闭包的方法. : – /

解决方法

你只需要告诉它闭包是一个闭包:

void everyPixel( Closure closure ){
  for( x in 0..<width )
    for( y in 0..<height )
      closure( x,y )
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读