groovy: how to pass varargs and closure in same time to a method?

后端 未结 3 1184
野性不改
野性不改 2021-02-20 11:39

Given following groovy function:

def foo(List params, Closure c) {...}

The method call would be:

foo([\'a\', \'b         


        
3条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-20 12:21

    I think this only could be possible if you use an array as argument or an Variable-Length Argument List :

    def foo(Object... params) {
        def closureParam = params.last()
        closureParam()
    }
    
    foo('a', 'b') { print "bar" }
    

提交回复
热议问题