I have the following Scala program:
object Test extends App {
val zip = (List(1, 3, 5), List(2, 4, 6)).zipped
val f: Tuple2[Int, Int] => Unit = x => println(x._1 + x._2)
zip.foreach(f)
}
Why I get the following compiler error:
Error:(6, 15) type mismatch;
found : ((Int, Int)) => Unit
required: (Int, Int) => ?
zip.foreach(f)
when there is an implicit conversion from Tuple2Zipped
to Traversable
where foreach[U](f: ((El1, El2)) => U): Unit
is defined.
NOTE: I edited my question to clarify.
I know that the foreach
defined in Tuple2Zipped
has the following signature:
def foreach[U](f: (El1, El2) => U): Unit
and that my function f
does not fit as the argument.
But in Tuple2Zipped.scala is defined the trait ZippedTraversable2
and his companion object this way:
trait ZippedTraversable2[+El1, +El2] extends Any {
def foreach[U](f: (El1, El2) => U): Unit
}
object ZippedTraversable2 {
implicit def zippedTraversable2ToTraversable[El1, El2](zz: ZippedTraversable2[El1, El2]): Traversable[(El1, El2)] = {
new scala.collection.AbstractTraversable[(El1, El2)] {
def foreach[U](f: ((El1, El2)) => U): Unit = zz foreach Function.untupled(f)
}
}
}
so as my function f
does match the argument of the foreach
defined in the Traversable
returned by zippedTraversable2ToTraversable
and the companion object defines an implicit conversion from ZippedTraversable2
to this Traversable
and Tuple2Zipped
is a ZippedTraversable2
, i think that this conversion has to be tried and my function be accepted.
The Intellij Idea editor accepts my construct without reporting any error but the compiler fails with the shown error.