[英]how to use closure for passing multiple argument in closure in groovy
below is my groovy code I want to pass multiple arguments in closure in groovy下面是我的 groovy 代码我想在 groovy 的闭包中传递多个 arguments
package com.test.demo
def str = {'Yash' , 'deep'}
def merged = { println "$str world"}
merged.call()
this code is giving me an error please anyone give me a solution how can I pass multiple arguments in closure in groovy这段代码给我一个错误请任何人给我一个解决方案如何在 groovy 中传递多个 arguments 闭包
There are two problems with your original code:您的原始代码有两个问题:
{ 'Yash', 'deep' }
does not imply returning an array, but this will give you an error: { 'Yash', 'deep' }
并不意味着返回一个数组,但这会给你一个错误:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
/tmp/x.groovy: 2: Unexpected input: ',' @ line 2, column 19.
def str = {'Yash' , 'deep'}
So you have to rewrite that closure to:因此,您必须将该闭包重写为:
{['Yash' , 'deep']}
(note the []
) (注意
[]
)
Next, if you print the closure like this, you would get the reference printed - so you need to call it.接下来,如果您像这样打印闭包,您将打印引用 - 所以您需要调用它。
def merged = { println "${it()} world"}
def str = {['Yash' , 'deep']}
merged(str)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.