summaryrefslogtreecommitdiff
path: root/src/library/scala/collection/immutable/Range.scala
diff options
context:
space:
mode:
authorSteve Robinson <steve.robinson290391@gmail.com>2016-02-27 09:13:45 -0800
committerSom Snytt <som.snytt@gmail.com>2016-05-19 10:54:33 -0700
commit214ea82573624bffb4d0f16f3e5c49f9370ba7a7 (patch)
tree906b6e646bceab8f43ab4812bcee48181149b3e3 /src/library/scala/collection/immutable/Range.scala
parent4a7f82c9047d04d79aa0fe4c0f8dc249ba221f76 (diff)
downloadscala-214ea82573624bffb4d0f16f3e5c49f9370ba7a7.tar.gz
scala-214ea82573624bffb4d0f16f3e5c49f9370ba7a7.tar.bz2
scala-214ea82573624bffb4d0f16f3e5c49f9370ba7a7.zip
SI-9656 Range.toString distinguishes Numeric step
For Range and NumericRange, toString will indicate the step if it is not 1. Additionally, indicate empty ranges and ranges which are not "exact". For a "mapped" range, used by `Range.Double`, toString includes the underlying range and the simple type of the step (to distinguish Double from BigDecimal).
Diffstat (limited to 'src/library/scala/collection/immutable/Range.scala')
-rw-r--r--src/library/scala/collection/immutable/Range.scala17
1 files changed, 8 insertions, 9 deletions
diff --git a/src/library/scala/collection/immutable/Range.scala b/src/library/scala/collection/immutable/Range.scala
index d3fe367e50..6eaf404fe8 100644
--- a/src/library/scala/collection/immutable/Range.scala
+++ b/src/library/scala/collection/immutable/Range.scala
@@ -396,22 +396,20 @@ extends scala.collection.AbstractSeq[Int]
case _ =>
super.equals(other)
}
- /** Note: hashCode can't be overridden without breaking Seq's
- * equals contract.
- */
- override def toString() = {
- val endStr =
- if (numRangeElements > Range.MAX_PRINT || (!isEmpty && numRangeElements < 0)) ", ... )" else ")"
- take(Range.MAX_PRINT).mkString("Range(", ", ", endStr)
+ /* Note: hashCode can't be overridden without breaking Seq's equals contract. */
+
+ override def toString = {
+ val preposition = if (isInclusive) "to" else "until"
+ val stepped = if (step == 1) "" else s" by $step"
+ val prefix = if (isEmpty) "empty " else if (!isExact) "inexact " else ""
+ s"${prefix}Range $start $preposition $end$stepped"
}
}
/** A companion object for the `Range` class.
*/
object Range {
- private[immutable] val MAX_PRINT = 512 // some arbitrary value
-
/** Counts the number of range elements.
* @pre step != 0
* If the size of the range exceeds Int.MaxValue, the
@@ -514,6 +512,7 @@ object Range {
// we offer a partially constructed object.
class Partial[T, U](f: T => U) {
def by(x: T): U = f(x)
+ override def toString = "Range requires step"
}
// Illustrating genericity with Int Range, which should have the same behavior