user79074
user79074

Reputation: 5280

Converting a set of parameterized classes to an IntMap

Wondering if someone can tell me the correct way to convert the Set below to an 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))

gives me

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

And

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

Gives me:

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[_])

Upvotes: 0

Views: 166

Answers (1)

Ionuț G. Stan
Ionuț G. Stan

Reputation: 179159

IntMap.apply accepts a varargs param, so you need to call it like this:

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

Upvotes: 1

Related Questions