将 Seq[String] 转换为 String*

Converting Seq[String] to String*

我有一个接受 String* 作为参数的函数。我正在实现另一个函数,该函数将 Seq[String](或字符串数​​组)作为参数,但需要使用该参数调用前一个函数。有什么办法可以转换吗?

def foo (s: String*) = {
    ...
}

def callFoo (s: Seq[String]) = {
    foo (s)     // this throws an error
}

foo函数可以调用为foo("string1", "string2", "string3")。但我只想调用 callFoo(Seq[String]) 函数并从 foo()

获取结果

您可以使用 _* 运算符使 Seq 适应 foo 期望的变量参数列表,如下所示:

def callFoo (s: Seq[String]) = {
    foo (s: _*)
}