From 92fcc53be94a107f3b560b855564bc39ba8f2372 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Fri, 23 Apr 2004 14:11:16 +0000 Subject: *** empty log message *** --- sources/scala/Ordered.scala | 1 + sources/scala/Predef.scala | 17 ++++++++++ sources/scalac/ast/TreeGen.java | 4 +-- sources/scalac/symtab/Definitions.java | 20 ++++++++---- test/files/pos/orderedpoints.scala | 26 +++++++++++++++ test/files/pos/viewtest3.scala | 59 ++++++++++++++++++++++++++++++++++ 6 files changed, 118 insertions(+), 9 deletions(-) create mode 100755 test/files/pos/orderedpoints.scala create mode 100644 test/files/pos/viewtest3.scala diff --git a/sources/scala/Ordered.scala b/sources/scala/Ordered.scala index 4ea0ce95a0..2a75d68923 100644 --- a/sources/scala/Ordered.scala +++ b/sources/scala/Ordered.scala @@ -30,3 +30,4 @@ trait Ordered[+a] { def >= [b >: a <% Ordered[b]](that: b): boolean = (this compareTo that) >= 0; } + diff --git a/sources/scala/Predef.scala b/sources/scala/Predef.scala index 217158cc56..f6cc14a52f 100644 --- a/sources/scala/Predef.scala +++ b/sources/scala/Predef.scala @@ -141,6 +141,23 @@ object Predef { case _ => -(y compareTo x) } } + def view[a <% Ordered[a]](x: Array[a]): Ordered[Array[a]] = new Ordered[Array[a]] { + def compareTo [b >: Array[a] <% Ordered[b]](y: b): int = y match { + case y1: Array[a] => compareArrays(x, y1); + case _ => -(y compareTo x) + } + private def compareArrays(xs: Array[a], ys: Array[a]): int = { + var i = 0; + while (i < xs.length && i < ys.length) { + if (xs(i) < ys(i)) return -1; + if (xs(i) > ys(i)) return 1; + i = i + 1 + } + if (i < xs.length) return 1 + else if (i < ys.length) return -1 + else 0 + } + } def view[A](xs: Array[A]): Seq[A] = new Seq[A] { def length = xs.length; diff --git a/sources/scalac/ast/TreeGen.java b/sources/scalac/ast/TreeGen.java index 405f14d618..9e08df3f56 100644 --- a/sources/scalac/ast/TreeGen.java +++ b/sources/scalac/ast/TreeGen.java @@ -838,7 +838,7 @@ public class TreeGen implements Kinds, Modifiers, TypeTags { /** Builds an empty list. */ public Tree mkNil(int pos) { - return mkGlobalRef(pos, definitions.NIL); + return mkGlobalRef(pos, definitions.NIL()); } /** Builds a list with given element type, head and tail. */ @@ -852,7 +852,7 @@ public class TreeGen implements Kinds, Modifiers, TypeTags { global.prevPhase(); return New( mkApplyTV( - mkPrimaryConstructorGlobalRef(pos, definitions.CONS_CLASS), + mkPrimaryConstructorGlobalRef(pos, definitions.CONS_CLASS()), new Type[]{element}, new Tree[]{head, tail})); } diff --git a/sources/scalac/symtab/Definitions.java b/sources/scalac/symtab/Definitions.java index 31a69e40be..f086914990 100644 --- a/sources/scalac/symtab/Definitions.java +++ b/sources/scalac/symtab/Definitions.java @@ -197,13 +197,21 @@ public class Definitions { return LIST_CLASS.staticType(element); } - /** The scala.Nil module */ - public final Symbol NIL; + /** The scala.Nil module + * evaluated on demand to make bootstrap possible. + */ + public final Symbol NIL() { + return getModule("scala.Nil"); + } - /** The scala.:: class */ - public final Symbol CONS_CLASS; + /** The scala.:: class + * evaluated on demand to make bootstrap possible. + */ + public final Symbol CONS_CLASS() { + return getClass("scala.$colon$colon"); + } public final Type CONS_TYPE(Type element) { - return CONS_CLASS.staticType(element); + return CONS_CLASS().staticType(element); } /** The scala.Array class */ @@ -628,8 +636,6 @@ public class Definitions { ITERATOR_CLASS = getClass("scala.Iterator"); SEQ_CLASS = getClass("scala.Seq"); LIST_CLASS = getClass("scala.List"); - NIL = getModule("scala.Nil"); - CONS_CLASS = getClass("scala.$colon$colon"); ARRAY_CLASS = getClass("scala.Array"); TYPE_CLASS = getClass("scala.Type"); CONSTRUCTEDTYPE_CLASS = getClass("scala.ConstructedType"); diff --git a/test/files/pos/orderedpoints.scala b/test/files/pos/orderedpoints.scala new file mode 100755 index 0000000000..145b0a8312 --- /dev/null +++ b/test/files/pos/orderedpoints.scala @@ -0,0 +1,26 @@ +class Point1(x: int) extends Object with Ordered[Point1] { + val xCoord = x; + def compareTo [b >: Point1 <% Ordered[b]](that: b): int = that match { + case that1: Point1 => this.xCoord.compareTo(that1.xCoord) + case _ => -that.compareTo(this) + } +} +class Point2(x: int, y: int) extends Point1(x) with Ordered[Point2] { + val yCoord = y; + def compareTo [b >: Point2 <% Ordered[b]](that: b): int = that match { + case that1: Point2 => + val r = super.compareTo(that1); + if (r == 0) this.yCoord.compareTo(that1.yCoord) else r + case _ => -that.compareTo(this) + } +} +object Test with Application { + val p1 = new Point1(1); + val q1 = new Point1(2); + System.out.println(p1 < q1); + val p2 = new Point2(1, 2); + val q2 = new Point2(1, 3); + System.out.println(p2 < q2); + System.out.println(p1 < q2); + System.out.println(p2 < q1); +} diff --git a/test/files/pos/viewtest3.scala b/test/files/pos/viewtest3.scala new file mode 100644 index 0000000000..dfed0dbf52 --- /dev/null +++ b/test/files/pos/viewtest3.scala @@ -0,0 +1,59 @@ +package testview; + +trait Tree[+a <% Ordered[a]] { + def insert[b >: a <% Ordered[b]](x: b): Tree[b]; + def elements: List[a] +} + +object Empty extends Tree[All] { + def insert[b >: All <% Ordered[b]](x: b): Tree[b] = new Node(x, Empty, Empty); + def elements: List[All] = List(); +} + +class Node[+a <% Ordered[a]](elem: a, l: Tree[a], r: Tree[a]) extends Tree[a] { + def insert[b >: a <% Ordered[b]](x: b): Tree[b] = + if (x == elem) this + else if (x < elem) new Node(elem, l insert x, r) + else new Node(elem, l, r insert x); + def elements: List[a] = + l.elements ::: List(elem) ::: r.elements +} + +case class Str(elem: String) extends Ordered[Str] { + def compareTo[b >: Str <% Ordered[b]](that: b): int = that match { + case that1: Str => this.elem compareTo that1.elem + case _ => -(that compareTo this) + } +} + +object Test { +// import O.view; + + private def toCharList(s: String): List[Char] = + if (s.length() == 0) List() + else s.charAt(0) :: toCharList(s.substring(1)); + + def main(args: Array[String]) = { + { + var t: Tree[String] = Empty; + for (val s <- args) { + t = t insert s + } + System.out.println(t.elements) + } + { + var t: Tree[Str] = Empty; + for (val s <- args) { + t = t insert Str(s) + } + System.out.println(t.elements) + } + { + var t: Tree[List[char]] = Empty; + for (val s <- args) { + t = t insert toCharList(s) + } + System.out.println(t.elements) + } + } +} -- cgit v1.2.3