summaryrefslogtreecommitdiff
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
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).
-rw-r--r--src/library/scala/collection/immutable/NumericRange.scala14
-rw-r--r--src/library/scala/collection/immutable/Range.scala17
-rw-r--r--test/files/jvm/serialization-new.check8
-rw-r--r--test/files/jvm/serialization.check8
-rw-r--r--test/files/run/t9656.check14
-rw-r--r--test/files/run/t9656.scala43
6 files changed, 84 insertions, 20 deletions
diff --git a/src/library/scala/collection/immutable/NumericRange.scala b/src/library/scala/collection/immutable/NumericRange.scala
index c8d7519254..fdf50960a3 100644
--- a/src/library/scala/collection/immutable/NumericRange.scala
+++ b/src/library/scala/collection/immutable/NumericRange.scala
@@ -161,6 +161,12 @@ extends AbstractSeq[T] with IndexedSeq[T] with Serializable {
override def isEmpty = underlyingRange.isEmpty
override def apply(idx: Int): A = fm(underlyingRange(idx))
override def containsTyped(el: A) = underlyingRange exists (x => fm(x) == el)
+
+ override def toString = {
+ def simpleOf(x: Any): String = x.getClass.getName.split("\\.").last
+ val stepped = simpleOf(underlyingRange.step)
+ s"${super.toString} (using $underlyingRange of $stepped)"
+ }
}
}
@@ -250,9 +256,11 @@ extends AbstractSeq[T] with IndexedSeq[T] with Serializable {
super.equals(other)
}
- override def toString() = {
- val endStr = if (length > Range.MAX_PRINT) ", ... )" else ")"
- take(Range.MAX_PRINT).mkString("NumericRange(", ", ", endStr)
+ override def toString = {
+ val empty = if (isEmpty) "empty " else ""
+ val preposition = if (isInclusive) "to" else "until"
+ val stepped = if (step == 1) "" else s" by $step"
+ s"${empty}NumericRange $start $preposition $end$stepped"
}
}
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
diff --git a/test/files/jvm/serialization-new.check b/test/files/jvm/serialization-new.check
index 1c5dd4828b..ca91ec1073 100644
--- a/test/files/jvm/serialization-new.check
+++ b/test/files/jvm/serialization-new.check
@@ -97,12 +97,12 @@ x = Queue(a, b, c)
y = Queue(a, b, c)
x equals y: true, y equals x: true
-x = Range(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
-y = Range(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
+x = Range 0 until 10
+y = Range 0 until 10
x equals y: true, y equals x: true
-x = NumericRange(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
-y = NumericRange(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
+x = NumericRange 0 until 10
+y = NumericRange 0 until 10
x equals y: true, y equals x: true
x = Map(1 -> A, 2 -> B, 3 -> C)
diff --git a/test/files/jvm/serialization.check b/test/files/jvm/serialization.check
index 1c5dd4828b..ca91ec1073 100644
--- a/test/files/jvm/serialization.check
+++ b/test/files/jvm/serialization.check
@@ -97,12 +97,12 @@ x = Queue(a, b, c)
y = Queue(a, b, c)
x equals y: true, y equals x: true
-x = Range(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
-y = Range(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
+x = Range 0 until 10
+y = Range 0 until 10
x equals y: true, y equals x: true
-x = NumericRange(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
-y = NumericRange(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
+x = NumericRange 0 until 10
+y = NumericRange 0 until 10
x equals y: true, y equals x: true
x = Map(1 -> A, 2 -> B, 3 -> C)
diff --git a/test/files/run/t9656.check b/test/files/run/t9656.check
new file mode 100644
index 0000000000..03e3ff3b5f
--- /dev/null
+++ b/test/files/run/t9656.check
@@ -0,0 +1,14 @@
+Range 1 to 10
+Range 1 to 10
+inexact Range 1 to 10 by 2
+Range 1 to 10 by 3
+inexact Range 1 until 10 by 2
+Range 100 to 100
+empty Range 100 until 100
+NumericRange 1 to 10
+NumericRange 1 to 10 by 2
+NumericRange 0.1 until 1.0 by 0.1
+NumericRange 0.1 until 1.0 by 0.1
+NumericRange 0.1 until 1.0 by 0.1 (using NumericRange 0.1 until 1.0 by 0.1 of BigDecimal)
+NumericRange 0 days until 10 seconds by 1 second
+empty NumericRange 0 days until 0 days by 1 second
diff --git a/test/files/run/t9656.scala b/test/files/run/t9656.scala
new file mode 100644
index 0000000000..3732719553
--- /dev/null
+++ b/test/files/run/t9656.scala
@@ -0,0 +1,43 @@
+
+import scala.math.BigDecimal
+
+object Test extends App {
+ println(1 to 10)
+ println(1 to 10 by 1)
+ println(1 to 10 by 2)
+ println(1 to 10 by 3)
+ println(1 until 10 by 2)
+ println(100 to 100)
+ println(100 until 100)
+
+ println(1L to 10L)
+ println(1L to 10L by 2)
+
+ // want to know if this is BigDecimal or Double stepping by BigDecimal
+ println(0.1 until 1.0 by 0.1)
+ println(Range.BigDecimal(BigDecimal("0.1"), BigDecimal("1.0"), BigDecimal("0.1")))
+ println(Range.Double(0.1, 1.0, 0.1))
+
+ import concurrent.duration.{SECONDS => Seconds, _}, collection.immutable.NumericRange
+ implicit val `duration is integerish`: math.Integral[FiniteDuration] = new math.Integral[FiniteDuration] {
+ def quot(x: scala.concurrent.duration.FiniteDuration,y: scala.concurrent.duration.FiniteDuration): scala.concurrent.duration.FiniteDuration = ???
+ def rem(x: scala.concurrent.duration.FiniteDuration,y: scala.concurrent.duration.FiniteDuration): scala.concurrent.duration.FiniteDuration = ???
+
+ // Members declared in scala.math.Numeric
+ def fromInt(x: Int): scala.concurrent.duration.FiniteDuration = Duration(x, Seconds)
+ def minus(x: scala.concurrent.duration.FiniteDuration,y: scala.concurrent.duration.FiniteDuration): scala.concurrent.duration.FiniteDuration = ???
+ def negate(x: scala.concurrent.duration.FiniteDuration): scala.concurrent.duration.FiniteDuration = ???
+ def plus(x: scala.concurrent.duration.FiniteDuration,y: scala.concurrent.duration.FiniteDuration): scala.concurrent.duration.FiniteDuration = ???
+ def times(x: scala.concurrent.duration.FiniteDuration,y: scala.concurrent.duration.FiniteDuration): scala.concurrent.duration.FiniteDuration = ???
+ def toDouble(x: scala.concurrent.duration.FiniteDuration): Double = ???
+ def toFloat(x: scala.concurrent.duration.FiniteDuration): Float = ???
+ def toInt(x: scala.concurrent.duration.FiniteDuration): Int = toLong(x).toInt
+ def toLong(x: scala.concurrent.duration.FiniteDuration): Long = x.length
+
+ // Members declared in scala.math.Ordering
+ def compare(x: scala.concurrent.duration.FiniteDuration,y: scala.concurrent.duration.FiniteDuration): Int =
+ x.compare(y)
+ }
+ println(NumericRange(Duration.Zero, Duration(10, Seconds), Duration(1, Seconds)))
+ println(NumericRange(Duration.Zero, Duration.Zero, Duration(1, Seconds)))
+}