@gourlaysama already explained why it does not compile, and @Chirlo provide an easy (and recommended) to work: SortedMap( list: _*).
I would like to suggest an alternative:
import collection.Traversable
import collection.generic.CanBuildFrom
implicit class RichPairTraversable[A,B]( t: Traversable[(A,B)] ) {
def toPairCol[Col[A,B]](implicit cbf: CanBuildFrom[Nothing, (A,B), Col[A, B]]): Col[A, B] = {
val b = cbf()
b.sizeHint(t)
b ++= t
b.result
}
}
Some test in REPL:
scala> List((1, "Fred"), (2, "Barney")).toPairCol[scala.collection.immutable.SortedMap]
res0: scala.collection.immutable.SortedMap[Int,String] = Map(1 -> Fred, 2 -> Barney)
scala> List((1, "Fred"), (2, "Barney")).toPairCol[scala.collection.immutable.HashMap]
res1: scala.collection.immutable.HashMap[Int,String] = Map(1 -> Fred, 2 -> Barney)
Now, I probably will not use it in production, given that doing is SortedMap( list: _* )not so difficult and does not require magic.