开发者

using variable length argument in scala

开发者 https://www.devze.com 2023-02-12 09:08 出处:网络
I know how to define a method with variable length argument: case class taxonomy(vocabularies:(String,Set[Strin开发者_运维百科g])*)

I know how to define a method with variable length argument:

  case class taxonomy(vocabularies:(String,Set[Strin开发者_运维百科g])*)

and client code is very clean:

  val terms=taxonomy("topics"->Set("economic","politic")
                   ,"tag"->Set("Libya","evolution")
                   )

but I want to Know how can I use this case class when I have a variable (instead of a Sequence of variable) like this:

val notFormattedTerms = Map("topics"->Set("economic","politic")
       ,"tag"->Set("Libya","evolution"))


taxonomy(notFormattedTerms.toSeq:_*)

With : _* you virtually transform a sequence argument so that it looks as if a several arguments had been passed to the variable length method. This transformation, however, only works for (ordered?) simple sequence types and, as in this case, not for a Map. Therefore, one will have to use an explicit toSeq before.

0

精彩评论

暂无评论...
验证码 换一张
取 消