문제

I have a class with a closure defined as:

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

But if I apply the @CompileStatic annotation to it, it won't compile (it did before I added the closure), with the message:

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.

How do I create a type signature for this so that it will compile statically? All my hits on Google so far say how to pass a closure, rather than how to define a method that accepts one. :-/

도움이 되었습니까?

해결책

You just need to tell it that closure is a Closure:

void everyPixel( Closure closure ){
  for( x in 0..<width )
    for( y in 0..<height )
      closure( x, y )
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top