scala:如何将扩展列表作为可变parameter passing给方法?
在scala中创build一个Map时,我调用Map(entities.map{e => e.id -> e}) ,我得到:
found : scala.collection.mutable.IndexedSeq[(Int, Entity)] required: (Int, Entity)
这是因为Map.apply的签名是: def apply[A, B](elems: (A, B)*): CC[A, B] ,它需要一个可变参数风格的参数。
有没有办法转换IndexedSeq以便它可以通过Map.apply接受?
试试这个: Map(entities.map{e => e.id -> e}:_*)
显式使用:_*作为可变参数input似乎工作。
或者这也应该工作:
entities.map{e => e.id -> e} toMap