Skip to content

Commit aa3b646

Browse files
author
extempore
committed
Fleshed out scala.math.Ordered documentation.
Contributed by josh marcus. git-svn-id: http://lampsvn.epfl.ch/svn-repos/scala/scala/trunk@25310 5e8d7ff9-d8ef-0310-90f0-a4852d11357a
1 parent 8eb4136 commit aa3b646

File tree

1 file changed

+64
-17
lines changed

1 file changed

+64
-17
lines changed

src/library/scala/math/Ordered.scala

Lines changed: 64 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,42 +8,89 @@
88

99
package scala.math
1010

11-
/** A trait for totally ordered data.
11+
/** A trait for data that have a single, natural ordering. See
12+
* [[scala.math.Ordering]] before using this trait for
13+
* more information about whether to use use [[scala.math.Ordering]] instead.
14+
*
15+
* Classes that implement this trait can be sorted with
16+
* [[scala.utils.Sorting]] and can be compared with standard comparison operators
17+
* (e.g. > and <).
1218
*
13-
* Note that since version 2006-07-24 this trait is no longer covariant
14-
* in `A`.
19+
* Ordered should be used for data with a single, natural ordering (like
20+
* integers) while Ordering allows for multiple ordering implementations.
21+
* An Ordering instance will be implicitly created if necessary.
22+
*
23+
* [[scala.math.Ordering]] is an alternative to this trait that allows multiple orderings to be defined for the same type.
24+
*
25+
* [[scala.math.PartiallyOrdered]] is an alternative to this trait for partially ordered data.
26+
*
27+
* For example, to create a simple class that implements Ordered and then sort it with [[scala.utils.Sorting]]:
28+
* {{{
29+
* class OrderedClass(n:Int) extends Ordered[OrderedClass] {
30+
* def compare(that: OrderedClass) = this.n - that.n
31+
* }
32+
*
33+
* val x = List(new MyClass(1), new MyClass(5), new MyClass(3))
34+
* val result = scala.utils.Sorting.quickSort(x)
35+
* }}}
36+
*
37+
* Some additional notes from the initial implementation:
38+
*
39+
* Note that since version 2006-07-24 this trait is no longer covariant in `A`.
1540
*
1641
* It is important that the `equals` method for an instance of `Ordered[A]`
17-
* be consistent with the compare method. However, due to limitations
18-
* inherent in the type erasure semantics, there is no reasonable way to
19-
* provide a default implementation of equality for instances of `Ordered[A]`.
20-
* Therefore, if you need to be able to use equality on an instance of
21-
* `Ordered[A]` you must provide it yourself either when inheriting or
22-
* instantiating.
42+
* be consistent with the compare method. However, due to limitations
43+
* inherent in the type erasure semantics, there is no reasonable way to
44+
* provide a default implementation of equality for instances of `Ordered[A]`.
45+
* Therefore, if you need to be able to use equality on an instance of
46+
* `Ordered[A]` you must provide it yourself either when inheriting or
47+
* instantiating.
2348
*
2449
* It is important that the `hashCode` method for an instance of `Ordered[A]`
25-
* be consistent with the `compare` method. However, it is not possible to
26-
* provide a sensible default implementation. Therefore, if you need to be
27-
* able compute the hash of an instance of `Ordered[A]` you must provide it
28-
* yourself either when inheriting or instantiating.
50+
* be consistent with the `compare` method. However, it is not possible to
51+
* provide a sensible default implementation. Therefore, if you need to be
52+
* able compute the hash of an instance of `Ordered[A]` you must provide it
53+
* yourself either when inheriting or instantiating.
2954
*
55+
* @see [[scala.math.Ordering]], [[scala.math.PartiallyOrdered]]
3056
* @author Martin Odersky
3157
* @version 1.1, 2006-07-24
3258
*/
3359
trait Ordered[A] extends java.lang.Comparable[A] {
3460

3561
/** Result of comparing `this` with operand `that`.
36-
* returns `x` where
37-
* `x &lt; 0` iff `this &lt; that`
38-
* `x == 0` iff `this == that`
39-
* `x &gt; 0` iff `this &gt; that`
62+
*
63+
* Implement this method to determine how instances of A will be sorted.
64+
*
65+
* Returns `x` where:
66+
*
67+
* - `x < 0` when `this > that`
68+
*
69+
* - `x == 0` when `this == that`
70+
*
71+
* - `x < 0` when `this > that`
72+
*
4073
*/
4174
def compare(that: A): Int
4275

76+
/** Returns true if `this` is less than `that`
77+
*/
4378
def < (that: A): Boolean = (this compare that) < 0
79+
80+
/** Returns true if `this` is greater than `that`.
81+
*/
4482
def > (that: A): Boolean = (this compare that) > 0
83+
84+
/** Returns true if `this` is less than or equal to `that`.
85+
*/
4586
def <= (that: A): Boolean = (this compare that) <= 0
87+
88+
/** Returns true if `this` is greater than or equal to `that`.
89+
*/
4690
def >= (that: A): Boolean = (this compare that) >= 0
91+
92+
/** Result of comparing `this` with operand `that`.
93+
*/
4794
def compareTo(that: A): Int = compare(that)
4895
}
4996

0 commit comments

Comments
 (0)