Inferred List Type with Either and Option
 In Scala 2.11.6, putting an Either and an Option into a List results in the inferred type, List[Object] :  
scala> val e: Either[String, Int] = Right(100)
e: Either[String,Int] = Right(100)
scala> val o: Option[Int] = None
o: Option[Int] = None
scala> List(e, o)
res0: List[Object] = List(Right(100), None)
 Either and Option both extend AnyRef , so why isn't it a List[AnyRef] ?  
 Why doesn't AnyRef show up as the Least Upper Bound?  
 Also, I was expecting a List[Any] .  Why does List[Object] show up?  
If Scala is used in the context of a Java runtime environment, then scala.AnyRef corresponds to java.lang.Object.
scala> val x: List[AnyRef] = List(e, o)
x: List[AnyRef] = List(Right(100), None)
scala> val x: List[java.lang.Object] = List(e, o)
x: List[Object] = List(Right(100), None)
 You expected the Least Upper Bound AnyRef and you got it.  The source of quote and diagram.  
 It is a list of AnyRef .  AnyRef corresponds to java.lang.Object .  From here:  
If Scala is used in the context of a Java runtime environment, then scala.AnyRef corresponds to java.lang.Object.
You can show just as much with reflection:
import scala.reflect.runtime.universe._
scala> lub(List(typeOf[Either[String, Int]], typeOf[Option[Int]]))
res43: reflect.runtime.universe.Type = Object
scala> typeOf[AnyRef] =:= typeOf[java.lang.Object]
res44: Boolean = true
scala> typeOf[AnyRef] =:= typeOf[Any]
res45: Boolean = false
上一篇: Scala类型检查器无法正确推断类型
下一篇: 推断列表类型与任一和选项
