Scala – List element datatypes

The list type in Scala is covariant. This means that for each pair of types S and T, if S is a subtype of T, then List[S] is a subtype of List[T].

Nothing is the bottom type in Scala’s class hierarchy. It is a subtype of every other Scala type. Because lists are covariant, it follows that List[Nothing] is a subtype of List[T], for any type T.

object Lists_Operations extends App {

  val lstNested: List[List[Int]] = List(List(1, 2, 3), List(4, 5, 6))
  println(lstNested)

  val lstNothing: List[Nothing] = List()
  val lstString: List[String] = List()

}

Leave a comment