Вопрос

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