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

时间:2013-03-15 02:28:30

标签: groovy closures groovy++

我有一个类,其闭包定义为:

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

但是如果我将@CompileStatic注释应用于它,它将无法编译(它在我添加闭包之前就已经完成),并带有以下消息:

  

Groovyc:[静态类型检查] - 找不到匹配方法java.lang.Object #call(java.lang.Integer,java.lang.Integer)。请检查声明的类型是否正确以及方法是否存在。

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

1 个答案:

答案 0 :(得分:5)

您只需告诉它closureClosure

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