Skip to content

Commit 129e3a7

Browse files
committed
Fixes Iterable#toString forcing elements
Fixes scala/bug#11785 This changes the implementation of Iterable#toString to not force the evaluation of its elements unless it extends StrictOptimizedIterableOps.
1 parent 6af3fb2 commit 129e3a7

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

library/src/scala/collection/Iterable.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,12 @@ trait Iterable[+A] extends IterableOnce[A]
6868
protected[this] def stringPrefix: String = "Iterable"
6969

7070
/** Converts this $coll to a string.
71-
*
72-
* @return a string representation of this collection. By default this
73-
* string consists of the `className` of this $coll, followed
74-
* by all elements separated by commas and enclosed in parentheses.
7571
*/
76-
override def toString = mkString(className + "(", ", ", ")")
72+
override def toString: String =
73+
this match {
74+
case _: StrictOptimizedIterableOps[_, _, _] => mkString(className + "(", ", ", ")")
75+
case _ => className + (if (isEmpty) "()" else "(<iterable>)")
76+
}
7777

7878
/** Analogous to `zip` except that the elements in each collection are not consumed until a strict operation is
7979
* invoked on the returned `LazyZip2` decorator.

library/src/scala/collection/concurrent/TrieMap.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,6 +1002,7 @@ final class TrieMap[K, V] private (r: AnyRef, rtupd: AtomicReferenceFieldUpdater
10021002
else cachedSize()
10031003
override def isEmpty: Boolean = size == 0
10041004
override protected[this] def className = "TrieMap"
1005+
override def toString: String = mkString(className + "(", ", ", ")")
10051006

10061007
}
10071008

0 commit comments

Comments
 (0)