diff options
author | Martin Odersky <odersky@gmail.com> | 2015-03-13 17:44:04 +0100 |
---|---|---|
committer | Martin Odersky <odersky@gmail.com> | 2015-03-13 17:45:03 +0100 |
commit | a56bc34fb850a6dca40c4f8562f3cb2d5b69dcc7 (patch) | |
tree | e9804c9402e33c35fb297bb6b03f456e7a472730 /tests/pos/viewtest1.scala | |
parent | 0ee93122b9e367c08ed1d81c9cfc5919fc3a32af (diff) | |
download | dotty-a56bc34fb850a6dca40c4f8562f3cb2d5b69dcc7.tar.gz dotty-a56bc34fb850a6dca40c4f8562f3cb2d5b69dcc7.tar.bz2 dotty-a56bc34fb850a6dca40c4f8562f3cb2d5b69dcc7.zip |
add/strict
Add -strict option to do some type checks that are encessary to ensure type soundness, but
are stricter than what Scala 2.x enforces.
The first such test is the "pattern cannot be uniquely instantiated" problem where we reject a
non-variant case subclass of a covariant superclass in a pattern match. The error is now only
issued in -struct mode, otherwise it will be a warning.
We might move more tests into the same category. This should help the transition.
Diffstat (limited to 'tests/pos/viewtest1.scala')
-rw-r--r-- | tests/pos/viewtest1.scala | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/tests/pos/viewtest1.scala b/tests/pos/viewtest1.scala new file mode 100644 index 000000000..38945ad2f --- /dev/null +++ b/tests/pos/viewtest1.scala @@ -0,0 +1,42 @@ +package test + +trait Ordered[a] { + def < (x: a): Boolean +} + +object O { + implicit def view (x: String): Ordered[String] = new Ordered[String] { + def < (y: String) = x.compareTo(y) < 0 + } +} + +object Empty extends Tree[Nothing] +case class Node[c <% Ordered[c]](elem: c, l: Tree[c], r: Tree[c]) extends Tree[c] + +abstract class Tree[+a <% Ordered[a]] { + def insert[b >: a <% Ordered[b]](x: b): Tree[b] = this match { + case Empty => + new Node(x, Empty, Empty) + case Node(elem, l, r) => + if (x == elem) this + else if (x < elem) Node(elem, l insert x, r) + else Node(elem, l, r insert x) + } + def elements: List[a] = this match { + case Empty => List() + case Node(elem, l, r) => + l.elements ::: List(elem) ::: r.elements + } +} + +object Test { + import O.view + + def main(args: Array[String]): Unit = { + var t: Tree[String] = Empty + for (s <- args) { + t = t insert s + } + println(t.elements) + } +} |