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.
reduceLeft requires the function block (inside the parentheses) to return the same type as the collection. This is because block is invoked recursively until all values of the collection are consumed.
stuff is a List[Int], but (_ +”, ” + _) is a String, hence the type error.
A similar method to reduceLeft is foldLeft. The distinction is that the collection type and the resultant type can be different. For example:
I guess your example is indicative, but if you really want a String of comma separated values, then the following would be better: