The first list is of type List[Double]
because of literal type widening. Scala sees the literals, and notes that, even though they are of different types, they can be unified by widening some of them. If there was no type widening, then the most common superclass, AnyVal
would be adopted.
List(1.1 /* Double literal */), 3 /* Int literal */)
The second list is clearly List[Int]
, though explicit calling for Double
will result in type widening for the literals.
List(5 /* Int literal */, 7 /* Int literal */)
Now, it's important to note that type widening is something that happens at compile time. The compiled code will not contain any Int
3, only Double
3.0. Once a list has been creater, however, it is not possible to do type widening, because the stored objects are, in fact, different.
So, once you concat the two lists, the resulting type will be a superclass of Double
and Int
. Namely, AnyVal
. As a result of Java interoperability, however, AnyVal
cannot contain any useful methods (such as numeric operators).
I do wonder what Clojure does internally. Does it convert integers into doubles when concatenating? Or does it store everything as Object
(like Scala does) but has smarter math operators?