将序列传递给不使用类型归属的varargs方法

ada*_*bsg 2 scala

我正在使用第三方库,正在尝试做一些抽象操作,并且遇到了一个接受varargs但未使用类型描述的方法的问题。就像是:

  def outer(otherStuff:String*): Unit ={
    if(someCondition)
      methodInThirdPartyLibrary(otherStuff)
    // other code....
  }

  def methodInThirdPartyLibrary(stuff:String*): Unit ={
    println(stuff.mkString(","))
  }
Run Code Online (Sandbox Code Playgroud)

给定库如何设置方法,是否有任何方法可以传递参数?

Krz*_*sik 5

您只需要使用特殊的:_*传播名称:

def outer(otherStuff:String*): Unit ={
   if(someCondition)
     methodInThirdPartyLibrary(otherStuff: _*)
   // other code....
}
Run Code Online (Sandbox Code Playgroud)

还要检查这个答案

  • 感谢您提供的链接,该链接称为smooch运算符。哈。哦,现在我也刚刚看到有人会误解它的去向。 (2认同)