First, here are 3 snippets of code along with their output on Scala 2.10.2
// 1.
def one: Seq[List[String]] =
Seq(List("x")) ++ List(List("x"))
println(one)
// => List(List(x), List(x)))
// 2.
def two: List[List[String]] =
Seq(List("x")) ++ List(List("x"))
println(two)
// =>
// error: type mismatch;
// found : Seq[List[String]]
// required: List[List[String]]
// Seq(List("x")) ++ List(List("x"))
// one error found
// 3.
println(Seq(List("x")) ++ List(List("x")))
// => List(List(x), List(x))
The main code in all the 3 snippets is the same -- Seq(List("x")) ++ List(List("x"))
The first and the third snippet show (print) the type as List[List[String]]
, but the second snippet, which specifies the return type as List[List[String]]
fails to compile. The first one's return type is Seq[List[String]]
but it's printed as a List[List[String]]
.
What exactly is happening here?