Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

using variable length argument in scala

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

  case class taxonomy(vocabularies:(String,Set[String])*)

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"))
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...