如何在不先赋值变量的情况下在 shell.parse() 上调用函数?例如 shell.parse().someFunc



现在我正在这样做:

//main.groovy
def func = shell.parse( new File('func.groovy') )
func.someMethod('sdfsdfsdfsdf')
//func.groovy
def someMethod(deploymentFolder) {
return deploymentFolder
}

我想使main.groovy中的片段成为单行代码,但这不起作用:

def func = shell.parse( new File('func.groovy') ).someMethod('sdfsdfsdf')

这也不起作用:

def func = shell.parse( new File('func.groovy') ) someMethod('sdfsdfsdf')

有没有办法直接在shell.parse像这样返回的函数上调用函数?

编辑

我在收集电话中调用它,这似乎改变了事情

所以这是行不通的:

list = arrList.collect { file ->
return shell.parse( new File(file) ).someMethod('sdfsdfsdf')
}

收集后,列表似乎包含正确数量的嵌套列表,但它们都是空的。

然而,这样做确实有效:

myarr = []
list = arrList.collect { file ->
tempVar = shell.parse( new File(file) )
myarr += tempVar.someMethod('sdfsdfsdf')
}

我不确定有什么区别。我以为收集也会做同样的事情。它似乎几乎做了同样的事情,但它连接的列表都是空的。

你的第一次尝试是正确的,并且像怀疑的那样工作:

def shell = new GroovyShell()
println(["func.groovy"].collect{ it as File }.collect{ shell.parse(it).someMethod('sdfsdfsdfsdf') })
// ⇒ [sdfsdfsdfsdf]

最新更新