Groovy 迭代器:无方法签名:适用于参数类型:(myFunction_closure1_closure3)

Groovy Iterators: No signature of method: is applicable for argument types: (myFunction_closure1_closure3)

我正在遍历一组字符串值并将每个字符串值传递给外部函数,如下所示:

List<String> myArray = ["a", "b", "c"]

myArray.each() {
  myExternalFunction({it})
}

但是,我遇到了上面的关闭错误。为什么会这样? {it} 在闭包内时是否计算为 String?为什么将闭包本身作为参数传递?

List<String> myArray = ["a", "b", "c"]

myArray.each {
  myExternalFunction(it)
}

myExternalFunction 除了一个 String 但在当前的实现中 {it} 是一个传递给方法的闭包。因此抱怨。

您可以找到更多关于 Closures in Groovy.