groovy 中的 splat 运算符?

发布于 2024-09-03 18:14:26 字数 497 浏览 3 评论 0原文

def foo(map, name) {
  println(map)
}

foo("bar", hi: "bye")

[hi:bye]

现在我有一张以前的地图,我希望将其传递给 foo 在伪代码中,类似:

def otherMap = [hi: "world"]
foo("bar", hi: "bye", otherMap*)

这样它就打印出来

[hi:world]

这当然是行不通的。

另外,尝试仅传递地图会混合参数的顺序:

def otherMap = [hi: "world"]
foo("bar", otherMap)

将打印

bar

如何解决此问题?

def foo(map, name) {
  println(map)
}

foo("bar", hi: "bye")

will print

[hi:bye]

Now I have a previous map that I wish to pass along to foo. In pseudo code, something like:

def otherMap = [hi: "world"]
foo("bar", hi: "bye", otherMap*)

So that it prints

[hi:world]

This doesn't work of course.

Also, trying to pass just the map mixes the order of arguments:

def otherMap = [hi: "world"]
foo("bar", otherMap)

will print

bar

How can I fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

叹梦 2024-09-10 18:14:26

您正在寻找展开地图运算符。

def foo(map, name) {
  println(map)
}

foo("bar", hi: "bye")

def otherMap = [hi: "world"]
foo("bar", hi: "bye", *:otherMap)
foo("bar", *:otherMap, hi: "bye")

印刷:

["hi":"bye"]
["hi":"world"]
["hi":"bye"]

You're looking for the spread-map operator.

def foo(map, name) {
  println(map)
}

foo("bar", hi: "bye")

def otherMap = [hi: "world"]
foo("bar", hi: "bye", *:otherMap)
foo("bar", *:otherMap, hi: "bye")

prints:

["hi":"bye"]
["hi":"world"]
["hi":"bye"]
最近可好 2024-09-10 18:14:26

我不确定您到底想要实现什么,所以这里有几种可能性:

如果您想将第二个地图中的内容添加到第一个地图中,则 leftShift 运算符是正确的方法:

def foo(name, map) {
  println(map)
}

def otherMap = [hi: "world"]
foo("bar", [hi: "bye"] << otherMap)

如果您想通过参数访问参数name 使用 Map:

def foo(Map args) {
  println args.map
}

def otherMap = [hi: "world"]
foo(name:"bar", first:[hi: "bye"], map:otherMap)

如果要打印所有参数或仅打印最后一个参数,请使用 varargs:

def printLast(Object[] args) {
  println args[-1]
}

def printAll(Object[] args) {
  args.each { println it }
}

def printAllButName(name, Map[] maps) {
  maps.each { println it }
}

def otherMap = [hi: "world"]
printLast("bar", [hi: "bye"], otherMap)
printAll("bar", [hi: "bye"], otherMap)
printAllButName("bar", [hi: "bye"], otherMap)

I'm not sure what you exactly want to achieve, so here are several possibilities:

If you want to add the contents from the second map to the first map, the leftShift operator is the way to go:

def foo(name, map) {
  println(map)
}

def otherMap = [hi: "world"]
foo("bar", [hi: "bye"] << otherMap)

If you want to access a parameter via its name use a Map:

def foo(Map args) {
  println args.map
}

def otherMap = [hi: "world"]
foo(name:"bar", first:[hi: "bye"], map:otherMap)

If you want to print all or only the last parameter use varargs:

def printLast(Object[] args) {
  println args[-1]
}

def printAll(Object[] args) {
  args.each { println it }
}

def printAllButName(name, Map[] maps) {
  maps.each { println it }
}

def otherMap = [hi: "world"]
printLast("bar", [hi: "bye"], otherMap)
printAll("bar", [hi: "bye"], otherMap)
printAllButName("bar", [hi: "bye"], otherMap)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文