diff options
Diffstat (limited to 'Scala/sandbox/7-variance.scala')
-rw-r--r-- | Scala/sandbox/7-variance.scala | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/Scala/sandbox/7-variance.scala b/Scala/sandbox/7-variance.scala new file mode 100644 index 0000000..49f0373 --- /dev/null +++ b/Scala/sandbox/7-variance.scala @@ -0,0 +1,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 + } +} |