I have written the following class that I later want to use according to the Pimp My Library pattern:
class ListX[A] (list: List[A]) {
def zipMap[A, B, C] (that: List[B], op: (A, B) => C): List[C] =
list.zip(that).map({
case (a: A, b: B) => op(a, b)
})
}
This compiles with warnings:
[warn] /src/main/scala/ListX.scala:8: abstract type pattern A is unchecked since it is eliminated by erasure
[warn] case (a: A, b: B) => op(a, b)
[warn] ^
[warn] /src/main/scala/ListX.scala:8: abstract type pattern B is unchecked since it is eliminated by erasure
[warn] case (a: A, b: B) => op(a, b)
[warn] ^
[warn] two warnings found
Testing it in REPL results in the following error:
scala> val a = new ListX(List(1, 1, 1))
scala> val b = List(1, 1, 1)
scala> val result = a.zipMap(b, _ + _)
error: missing parameter type for expanded function ((x$1: <error>, x$2) => x$1.$plus(x$2))
scala> val expected = List(2, 2, 2)
Since I am new to Scala I do not fully understand the warnings and the error. I know that there is this thing called "type erasure", but not how it works, and I can see that this probably leads to a missing type.
So what is wrong and how can I correct it?
Update:
Thanks to the accepted answer and its comments I managed to fix the issues and rewrote the class as follows:
implicit class ListX[A] (list: List[A]) {
def zipMap[B, C] (that: List[B])(op: (A, B) => C): List[C]
= list.zip(that).map({ op.tupled })
}