Scala 2 uses weak conformance relation when inferring the least upper bound of primitive number types such that
List('a', 2)
is typed to List[Int]
instead of, perhaps expected, List[AnyVal]
. The compiler inserts toInt
List('a'.toInt, 2)
as explained by SLS 6.26.1 Value Conversions
If has a primitive number type which weakly conforms to the
expected type, it is widened to the expected type using one of the
numeric conversion methods toShort
, toChar
, toInt
, toLong
, toFloat
,
toDouble
defined in the standard library.
Weak conformance seems to be a dropped feature in Scala 3, for example,
Starting dotty REPL...
scala> val a = 'a'
| val i = 2
val a: Char = a
val i: Int = 2
scala> List(a, i)
val res0: List[AnyVal] = List(a, 2)
where we see dotty deduced List[AnyVal]
. If we try to force the type to List[Int]
then warning is raised
scala> val l: List[Int] = List(a,i)
1 |val l: List[Int] = List(a,i)
| ^
|Use of implicit conversion method char2int in object Char should be enabled
|by adding the import clause 'import scala.language.implicitConversions'
|or by setting the compiler option -language:implicitConversions.
|See the Scala docs for value scala.language.implicitConversions for a discussion
|why the feature should be explicitly enabled.
val l: List[Int] = List(97, 2)