summaryrefslogtreecommitdiffstats
path: root/Scala/sandbox/7-variance.scala
blob: 49f037353cb11f162590704e06770704ab529243 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
object Variance
{
  // to allow 
  trait List[+T] {
    def isEmpty: Boolean
    def head: T
    def tail: List[T]
  }

  class Cons[T](val head: T, val tail: List[T]) extends List[T] {
    def isEmpty = false
  }

  // extends List[Nothing] so that it can hold anything
  object Nil extends List[Nothing] {
    def isEmpty = true
    def head: Nothing = throw new NoSuchElementException("Nil.head")
    def tail: Nothing = throw new NoSuchElementException("Nil.tail")
  }

  def run = {
    println("Variance")
    val x: List[String] = Nil
  }
}