斯卡拉:无所事事

时间:2015-03-24 11:13:11

标签: scala types

据说scala.Nothingscala.Null是底层类,它们扩展了所有其他AnyRef类。请考虑以下代码段

class Test() {}
val test:Test = null

因此,对于成功的语句,Null应该扩展自定义类Test(即Test是超类型的Null),或者类型系统应该为{不抛出类型不匹配错误的例外} {1}}。 scala如何确保这两个类总是扩展scala中的任何其他AnyRef后代类?

1 个答案:

答案 0 :(得分:3)

通常编译器显式处理底部类型一致性,例如the excerpt from scala compiler

...
} else if (isNullType) {
  if (other.isNothingType) false
  else if (other.isPrimitive) false
  else true // Null conforms to all classes (except Nothing) and arrays.
} else if (isNothingType) {
  true
} else other match {
...