diff options
-rw-r--r-- | config/list/library.lst | 4 | ||||
-rw-r--r-- | doc/reference/ExamplesPart.tex | 23 | ||||
-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 |
7 files changed, 83 insertions, 19 deletions
diff --git a/config/list/library.lst b/config/list/library.lst index e778870cd3..d21ad26cd7 100644 --- a/config/list/library.lst +++ b/config/list/library.lst @@ -36,8 +36,8 @@ Nil.scala None.scala Option.scala Ord.scala -#Ordered.scala -#PartiallyOrdered.scala +Ordered.scala +PartiallyOrdered.scala PartialFunction.scala Predef.scala Ref.java diff --git a/doc/reference/ExamplesPart.tex b/doc/reference/ExamplesPart.tex index f065da8978..140544745d 100644 --- a/doc/reference/ExamplesPart.tex +++ b/doc/reference/ExamplesPart.tex @@ -3298,7 +3298,7 @@ def product(xs: List[int]) = (1 :: xs) reduceLeft {(x, y) => x * y} Here is the implementation of \code{reduceLeft}. \begin{lstlisting} def reduceLeft(op: (a, a) => a): a = this match { - case Nil => error("Nil.reduceLeft") + case Nil => throw new Error("Nil.reduceLeft") case x :: xs => (xs foldLeft x)(op) } def foldLeft[b](z: b)(op: (b, a) => b): b = this match { @@ -3333,7 +3333,7 @@ List(x$_1$, ..., x$_n$).reduceRight(op) = x$_1$ op ( ... (x$_{n-1}$ op x$_n These are defined as follows. \begin{lstlisting} def reduceRight(op: (a, a) => a): a = match - case Nil => error("Nil.reduceRight") + case Nil => throw new Error("Nil.reduceRight") case x :: Nil => x case x :: xs => op(x, xs.reduceRight(op)) } @@ -4108,10 +4108,10 @@ and then translation continues with the latter expression. \end{itemize} For instance, taking our "pairs of integers whose sum is prime" example: \begin{lstlisting} -for { val i <- range(1, n); +for ( val i <- range(1, n); val j <- range(1, i); isPrime(i+j) -} yield (i, j) +) yield (i, j) \end{lstlisting} Here is what we get when we translate this expression: \begin{lstlisting} @@ -4153,8 +4153,8 @@ def flatten(xss: List[List[a]]): List[a] = \begin{exercise} Translate \begin{lstlisting} -for { val b <- books; val a <- b.authors; a startsWith "Bird" } yield b.title -for { val b <- books; (b.title indexOf "Program") >= 0 } yield b.title +for ( val b <- books; val a <- b.authors; a startsWith "Bird" ) yield b.title +for ( val b <- books; (b.title indexOf "Program") >= 0 ) yield b.title \end{lstlisting} to higher-order functions. \end{exercise} @@ -4314,7 +4314,7 @@ class BankAccount { if (0 < amount && amount <= balance) { balance = balance - amount; balance - } else error("insufficient funds"); + } else throw new Error("insufficient funds"); } \end{lstlisting} The class defines a variable \code{balance} which contains the current @@ -4344,7 +4344,6 @@ val account : BankAccount = BankAccount$\Dollar$class@1797795 10: scala.Int > account withdraw 15 java.lang.RuntimeException: insufficient funds - at error(Predef.scala:3) at BankAccount$\Dollar$class.withdraw(bankaccount.scala:13) at <top-level>(console:1) > @@ -4986,7 +4985,7 @@ together all iterators returned from successive calls of \code{f}. def next: b = if (cur.hasNext) cur.next else if (Iterator.this.hasNext) { cur = f(Iterator.this.next); next } - else error("next on empty iterator"); + else throw new Error("next on empty iterator"); } \end{lstlisting} Closely related to \code{map} is the \code{foreach} method, which @@ -5056,7 +5055,7 @@ always returns an empty sequence: object Iterator { object empty extends Iterator[All] { def hasNext = false; - def next: a = error("next on empty iterator"); + def next: a = throw new Error("next on empty iterator"); } \end{lstlisting} A more interesting iterator enumerates all elements of an array. This @@ -5068,7 +5067,7 @@ iterator is constructed by the \code{fromArray} method, which is also defined in i < xs.length; def next: a = if (i < xs.length) { val x = xs(i) ; i = i + 1 ; x } - else error("next on empty iterator"); + else throw new Error("next on empty iterator"); } \end{lstlisting} Another iterator enumerates an integer interval. The @@ -6065,7 +6064,7 @@ recursive functions. Syntax: \begin{lstlisting} letrec ident "=" term in term . \end{lstlisting} -The typing of \code{letrec} is as for {let}, +The typing of \code{letrec} is as for \code{let}, except that the defined identifier is visible in the defining expression. Using \code{letrec}, the \code{length} function for lists can now be defined as follows. \begin{lstlisting} letrec length = \xs. 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 = { |