Search code examples
scalatype-erasure

Avoid Elimination by Erasure while mapping over Map


I want to map over a Map that has the type List[~(A,Option[B])] but before I map over it I group it by A. Now that I can map over it, I have to match the Tuple of the Map:

val rawData: List[A ~ Option[B]]

rawData
    .groupBy(_._1)
    .map(case (first: A, second: Seq[A ~ Option[B]]) => 
        C(first, second.map(_._2))
    )

Now the Compiler warns me:

non-variable type argument anorm.~[A,Option[B]] in type pattern Seq[anorm.~[A,Option[B]]] is unchecked since it is eliminated by erasure

I found several solutions to make that matching possible, but I have the feeling, that it could be also possible to avoid the matching at all as I only want to go through that Map that has already a defined Type. How could this be possible?


Solution

  • In this case you actually don't have to worry about this. The error is because your case statement is too verbose. Change to the following:

    rawData.groupBy(_._1).map(case (first, second) => 
      C(first, second.map(_._2))
    )
    

    The types in the case statement restrict the type of the tuple (which is unnecessary). However, they restrict it in a way which is not verifiable at runtime (due to type erasure), that's why you get the error.