Home > Enterprise >  Why does this match type employing object types equate to Nothing?
Why does this match type employing object types equate to Nothing?

Time:09-17

In Dean Wampler's book, Programming Scala, 3rd Edition, there is an example with:

type Elem[X] = X match
  case String => Char
  case IterableOnce[t] => t
  case Array[t] => t
  case ? => X 

summon[Elem[List[Int]] =:= Int]
summon[Elem[Nil.type] =:= Nothing] 

It doesn't seem to be explained, at least in the surrounding context, why summon[Elem[Nil.type] =:= Nothing] and not summon[Elem[Nil.type] =:= Nil.type]. Why is this the case?

CodePudding user response:

As suggested in the comments, Nil extends List[Nothing], thus your case IterableOnce[t] => t applies and Elem[Nil.type] =:= Nothing.

  • Related