Here is the POC code:
object TypeTest extends Application {
val stuff = List(1,2,3,4,5)
def joined:String = stuff.reduceLeft(_ + ", " + _)
println(joined)
}
Upon compiling, it gives the following error:
tt.scala:4: error: type mismatch;
found : java.lang.String
required: Int
def joined:String = stuff.reduceLeft(_ + ", " + _)
^
tt.scala:4: error: type mismatch;
found : Int
required: String
def joined:String = stuff.reduceLeft(_ + ", " + _)
^
Writing the joined function like
reduceLeft(_.toString + ", " + _.toString)
does not help, still gives the same error. However, if I write it like
def joined:String = stuff.map(_.toString).reduceLeft(_ + ", " + _)
everything is fine.
Can someone please explain this weird combination of type errors? What is really going on here? The second one is especially weird, since there is an implicit conversion for Int to String.