summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/library/scala/collection/Iterator.scala3
-rw-r--r--src/library/scala/collection/TraversableLike.scala6
-rw-r--r--src/library/scala/collection/TraversableOnce.scala14
-rw-r--r--test/files/run/bug3516.check3
-rw-r--r--test/files/run/bug3516.scala13
5 files changed, 32 insertions, 7 deletions
diff --git a/src/library/scala/collection/Iterator.scala b/src/library/scala/collection/Iterator.scala
index daa6a59f3b..b8dd03110d 100644
--- a/src/library/scala/collection/Iterator.scala
+++ b/src/library/scala/collection/Iterator.scala
@@ -1007,6 +1007,9 @@ trait Iterator[+A] extends TraversableOnce[A] {
def toTraversable: Traversable[A] = toStream
def toIterator: Iterator[A] = self
+ def toStream: Stream[A] =
+ if (self.hasNext) Stream.cons(self.next, self.toStream)
+ else Stream.empty[A]
/** Converts this iterator to a string.
* @return `"empty iterator"` or `"non-empty iterator"`, depending on whether or not the iterator is empty.
diff --git a/src/library/scala/collection/TraversableLike.scala b/src/library/scala/collection/TraversableLike.scala
index 50259a1ee8..88a6379da2 100644
--- a/src/library/scala/collection/TraversableLike.scala
+++ b/src/library/scala/collection/TraversableLike.scala
@@ -696,6 +696,12 @@ trait TraversableLike[+A, +Repr] extends HasNewBuilder[A, Repr] with Traversable
def toTraversable: Traversable[A] = thisCollection
def toIterator: Iterator[A] = toIterable.iterator
+ /** Converts this $coll to a stream.
+ * $willNotTerminateInf
+ * @return a stream containing all elements of this $coll.
+ */
+ def toStream: Stream[A] = toIterable.toStream
+
/** Converts this $coll to a string.
* @return a string representation of this collection. By default this
* string consists of the `stringPrefix` of this $coll,
diff --git a/src/library/scala/collection/TraversableOnce.scala b/src/library/scala/collection/TraversableOnce.scala
index 3984264168..1797ff39db 100644
--- a/src/library/scala/collection/TraversableOnce.scala
+++ b/src/library/scala/collection/TraversableOnce.scala
@@ -66,6 +66,12 @@ trait TraversableOnce[+A] {
*/
def toTraversable: Traversable[A]
+ /** Converts this $coll to a stream.
+ * $willNotTerminateInf
+ * @return a stream containing all elements of this $coll.
+ */
+ def toStream: Stream[A]
+
/** Presently these are abstract because the Traversable versions use
* breakable/break, and I wasn't sure enough of how that's supposed to
* function to consolidate them with the Iterator versions.
@@ -383,7 +389,7 @@ trait TraversableOnce[+A] {
copyToArray(result, 0)
result
}
- else toStream.toArray
+ else toBuffer.toArray
}
/** Converts this $coll to a list.
@@ -416,12 +422,6 @@ trait TraversableOnce[+A] {
*/
def toBuffer[B >: A]: mutable.Buffer[B] = new ArrayBuffer[B] ++= self
- /** Converts this $coll to a stream.
- * $willNotTerminateInf
- * @return a stream containing all elements of this $coll.
- */
- def toStream: Stream[A] = toList.toStream
-
/** Converts this $coll to a set.
* $willNotTerminateInf
* @return a set containing all elements of this $coll.
diff --git a/test/files/run/bug3516.check b/test/files/run/bug3516.check
new file mode 100644
index 0000000000..d0d10d82fa
--- /dev/null
+++ b/test/files/run/bug3516.check
@@ -0,0 +1,3 @@
+1
+1
+21
diff --git a/test/files/run/bug3516.scala b/test/files/run/bug3516.scala
new file mode 100644
index 0000000000..aa302ce85a
--- /dev/null
+++ b/test/files/run/bug3516.scala
@@ -0,0 +1,13 @@
+object Test {
+ def mkIterator = (1 to 5).iterator map (x => { println(x) ; x })
+ def mkInfinite = Iterator continually { println(1) ; 1 }
+
+ def main(args: Array[String]): Unit = {
+ // Stream is strict in its head so we should see 1 from each of them.
+ val s1 = mkIterator.toStream
+ val s2 = mkInfinite.toStream
+ // back and forth without slipping into nontermination.
+ println((Stream from 1).toIterator.drop(10).toStream.drop(10).toIterator.next)
+ ()
+ }
+}