diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2013-04-25 17:17:17 +0200 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2016-11-10 18:03:23 +0100 |
commit | 82dc27578a2f57646f5b8c678699980b29b89c11 (patch) | |
tree | cc95bc8a9453992e4296a7fcfdba193830343d02 | |
parent | 87601a2e9c3fe2777b9f5b3f2be702998c167d5b (diff) | |
download | coursera-82dc27578a2f57646f5b8c678699980b29b89c11.zip coursera-82dc27578a2f57646f5b8c678699980b29b89c11.tar.gz |
Scala : sandbox: Scala : add 7-variance
-rw-r--r-- | Scala/sandbox/0-main.scala | 1 | ||||
-rw-r--r-- | Scala/sandbox/7-variance.scala | 25 |
2 files changed, 26 insertions, 0 deletions
diff --git a/Scala/sandbox/0-main.scala b/Scala/sandbox/0-main.scala index 99ee4ba..6f0edf3 100644 --- a/Scala/sandbox/0-main.scala +++ b/Scala/sandbox/0-main.scala @@ -9,5 +9,6 @@ object Main extends App { IntSet.run IntList.run Natural.run + Variance.run } 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 + } +} |