如何将Scala数组传递给Scala vararg方法?


76

考虑下面的代码:

private def test(some:String*){

}

private def call () {
  val some = Array("asd", "zxc")
  test(some)
}

expect String, found Array[String]为什么打印?Scala varargs不是数组吗?

注意

我在Stack Overflow上发现了一些有关Scala变量变量的问题,但所有这些问题都与调用Java varargs方法或将Scala列表转换为数组有关。

Answers:


122

像这样附加:_*到参数中test

test(some:_*)

它应该可以按预期工作。

如果您想知道魔术有什么用:_*,请参阅此问题


By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.