2013-03-15 69 views
3

我有一個關閉一個類定義爲:如何在Groovy中定義@CompileStatic兼容閉包?

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上的所有點擊說如何傳遞閉包,而不是如何定義接受方法。 : -/

回答

4

你只需要告訴它closureClosure

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