3
votes

It is said that scala.Nothing and scala.Null are bottom classes and they extend every other AnyRef classes. consider the below snippet

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

So for the statement to succeeded either Null should extend the custom class Test (i.e. Test is a super type of Null) or the type system should make exceptions of not throwing type mismatch error for scala.Null. How scala ensures that these two classes always extend any other AnyRef descendants classes in scala ?

1
Null and Nothing are explicitly special-cased in the compiler AIUI - is that what you're asking? - lmm
yes @Imm any idea how it works? does compiler only checks if the LHS is AnyRef descendant and doesn't follow the conventional type check? - rogue-one
Something like this: github.com/scala/scala/blob/… - dmitry
@dmitry yes exactly. I would accept your answer. - rogue-one

1 Answers

3
votes

Typically compiler explicitly handles bottom type conformity, like in 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 {
...