scala:如何将扩展列表作为varargs传递给方法?

dsg*_*dsg 50 scala map variadic-functions

Map在scala中创建时,我打电话给我Map(entities.map{e => e.id -> e}),然后我得到:

found   : scala.collection.mutable.IndexedSeq[(Int, Entity)]
required: (Int, Entity)
Run Code Online (Sandbox Code Playgroud)

这是因为签名Map.apply是:def apply[A, B](elems: (A, B)*): CC[A, B],这需要一个varargs样式参数.

有没有办法转换,IndexedSeq以便可以通过Map.apply

dsg*_*dsg 94

试试这个: Map(entities.map{e => e.id -> e}:_*)

明确地将其键入为varargs使用:_*似乎工作.


Ant*_*jdr 7

或者这也应该工作:

entities.map{e => e.id -> e} toMap
Run Code Online (Sandbox Code Playgroud)