diff options
author | Martin Odersky <odersky@gmail.com> | 2004-04-22 17:08:37 +0000 |
---|---|---|
committer | Martin Odersky <odersky@gmail.com> | 2004-04-22 17:08:37 +0000 |
commit | 09ce120614b6dcf4de255f1d33ec90d2498c2256 (patch) | |
tree | 0be4a85b2773ac95a60d9654502d54f08b4f4577 /sources | |
parent | f78573782b7ae0ee5f819d5498e56e3a48c6dab7 (diff) | |
download | scala-09ce120614b6dcf4de255f1d33ec90d2498c2256.tar.gz scala-09ce120614b6dcf4de255f1d33ec90d2498c2256.tar.bz2 scala-09ce120614b6dcf4de255f1d33ec90d2498c2256.zip |
*** empty log message ***
Diffstat (limited to 'sources')
-rw-r--r-- | sources/scala/List.scala | 2 | ||||
-rw-r--r-- | sources/scala/Ordered.scala | 22 | ||||
-rw-r--r-- | sources/scala/PartiallyOrdered.scala | 36 | ||||
-rw-r--r-- | sources/scala/Predef.scala | 12 | ||||
-rw-r--r-- | sources/scala/tools/scalac/typechecker/Infer.scala | 3 |
5 files changed, 70 insertions, 5 deletions
diff --git a/sources/scala/List.scala b/sources/scala/List.scala index 94341862c8..d0c61f78d0 100644 --- a/sources/scala/List.scala +++ b/sources/scala/List.scala @@ -160,6 +160,7 @@ object List { /** Lists with ordered elements are ordered * not yet since not compilable with bootstrap + */ def view[a <% Ordered[a]](x: List[a]): Ordered[List[a]] = new Ordered[List[a]] { def compareTo [b >: List[a] <% Ordered[b]](y: b): int = y match { case y1: List[a] => compareLists(x, y1); @@ -176,7 +177,6 @@ object List { } } } - */ } /** A trait representing an ordered collection of elements of type diff --git a/sources/scala/Ordered.scala b/sources/scala/Ordered.scala new file mode 100644 index 0000000000..11e23a4e98 --- /dev/null +++ b/sources/scala/Ordered.scala @@ -0,0 +1,22 @@ +package scala; + +/** A trait for totally ordered data. + */ +trait Ordered[+a] { + + /** Result of comparing `this' with operand `that'. + * returns `x' where + * x < 0 iff this < that + * x == 0 iff this == that + * x > 0 iff this > that + */ + def compareTo [b >: a <% Ordered[b]](that: b): int; + + def < [b >: a <% Ordered[b]](that: b): boolean = (this compareTo that) < 0; + + def > [b >: a <% Ordered[b]](that: b): boolean = (this compareTo that) > 0; + + def <= [b >: a <% Ordered[b]](that: b): boolean = (this compareTo that) <= 0; + + def >= [b >: a <% Ordered[b]](that: b): boolean = (this compareTo that) >= 0; +} diff --git a/sources/scala/PartiallyOrdered.scala b/sources/scala/PartiallyOrdered.scala new file mode 100644 index 0000000000..3cefb668c5 --- /dev/null +++ b/sources/scala/PartiallyOrdered.scala @@ -0,0 +1,36 @@ +package scala; + +/** A trait for partially ordered data. + */ +trait PartiallyOrdered[+a] { + + /** Result of comparing `this' with operand `that'. + * Returns `None' if operands are not comparable. + * If operands are comparable, returns `Some(x)' where + * x < 0 iff this < that + * x == 0 iff this == that + * x > 0 iff this > that + */ + def tryCompareTo [b >: a <% PartiallyOrdered[b]](that: b): Option[int]; + + def < [b >: a <% PartiallyOrdered[b]](that: b): boolean = + (this tryCompareTo that) match { + case Some(x) if x < 0 => true + case _ => false + } + def > [b >: a <% PartiallyOrdered[b]](that: b): boolean = + (this tryCompareTo that) match { + case Some(x) if x > 0 => true + case _ => false + } + def <= [b >: a <% PartiallyOrdered[b]](that: b): boolean = + (this tryCompareTo that) match { + case Some(x) if x <= 0 => true + case _ => false + } + def >= [b >: a <% PartiallyOrdered[b]](that: b): boolean = + (this tryCompareTo that) match { + case Some(x) if x >= 0 => true + case _ => false + } +} diff --git a/sources/scala/Predef.scala b/sources/scala/Predef.scala index 21c9ba1820..4063a32f64 100644 --- a/sources/scala/Predef.scala +++ b/sources/scala/Predef.scala @@ -74,7 +74,6 @@ object Predef { } // views ------------------------------------------------------------- -/* not yet compilable with bootstrap def view(x: int): Ordered[int] = new Ordered[int] { def compareTo [b >: int <% Ordered[b]](y: b): int = y match { @@ -85,6 +84,15 @@ object Predef { case _ => -(y compareTo x) } } + def view(x: char): Ordered[char] = new Ordered[char] { + def compareTo [b >: char <% Ordered[b]](y: b): int = y match { + case y1: char => + if (x < y1) -1 + else if (x > y1) 1 + else 0 + case _ => -(y compareTo x) + } + } def view(x: long): Ordered[long] = new Ordered[long] { def compareTo [b >: long <% Ordered[b]](y: b): int = y match { case y1: long => @@ -127,7 +135,5 @@ object Predef { case _ => -(y compareTo x) } } - -*/ } diff --git a/sources/scala/tools/scalac/typechecker/Infer.scala b/sources/scala/tools/scalac/typechecker/Infer.scala index 438a28afd5..1d4484d85d 100644 --- a/sources/scala/tools/scalac/typechecker/Infer.scala +++ b/sources/scala/tools/scalac/typechecker/Infer.scala @@ -318,7 +318,8 @@ class Infer(global: scalac_Global, gen: TreeGen, make: TreeFactory) extends scal } private def getViews(tp: Type): List[View] = { - memberViews(tp) ::: getContext.viewMeths; + //System.out.println("view for " + tp + " = " + (memberViews(tp) ::: getContext.viewMeths));//DEBUG + memberViews(tp) ::: getContext.viewMeths } def viewExpr(pos: int, v: View): Tree = { |