如何将接口作为varargs参数传递给Groovy中的方法?

ksh*_*p92 1 groovy variadic-functions

有没有办法将接口作为varargs参数传递给groovy中的方法?

这是我正在尝试做的事情:

interface Handler {
    void handle(String)
}

def foo(Handler... handlers) {
    handlers.each { it.handle('Hello!') }
}

foo({ print(it) }, { print(it.toUpperCase()) })
Run Code Online (Sandbox Code Playgroud)

当我运行以下代码时,我收到错误: No signature of method: ConsoleScript8.foo() is applicable for argument types: (ConsoleScript8$_run_closure1, ConsoleScript8$_run_closure2) values: [ConsoleScript8$_run_closure1@4359df7, ConsoleScript8$_run_closure2@4288c46b]

我需要改变什么?

cfr*_*ick 5

Java风格的...-varargs仅Handler[]适用于JVM.因此,实现这项工作的最短途径是:

foo([{ print(it) }, { print(it.toUpperCase()) }] as Handler[])
Run Code Online (Sandbox Code Playgroud)

(将它们作为列表投射到Handler[])