将一组参数化类转换为IntMap

时间:2014-09-12 09:44:30

标签: scala

想知道是否有人可以告诉我将下面的Set转换为IntMap的正确方法。

    case class A[T](i: Int, x: T)
    val set: Set[A[_]] = Set(A(1, 'x'), A(2, 3))

    val map: IntMap[A[_]] = IntMap(set.map(a => a.i -> a))

给了我

type mismatch; found : scala.collection.immutable.Set[(Int, A[_$19]) forSome { type _$19 }] required: (scala.collection.immutable.IntMapUtils.Int, A[_])

    val map: IntMap[A[_]] = IntMap(set.map(a => a.i -> a).toMap)

给我:

Cannot prove that (Int, A[_$19]) forSome { type _$19 } <:< (T, U).
    - not enough arguments for method toMap: (implicit ev: <:<[(Int, A[_$19]) forSome { type _$19 },(T, 
     U)])scala.collection.immutable.Map[T,U]. Unspecified value parameter ev.
    - polymorphic expression cannot be instantiated to expected type; found : [T, U]scala.collection.immutable.Map[T,U] required: 
     (scala.collection.immutable.IntMapUtils.Int, A[_])

1 个答案:

答案 0 :(得分:1)

IntMap.apply接受varargs param,所以你需要像这样调用它:

val map: IntMap[A[_]] = IntMap(set.map(a => a.i -> a).toSeq: _*)