Skip to content

to(Seq) doesn't evaluate collection when called on a Seq #238

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Aug 1, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,47 @@

package scala.collection.compat

import scala.reflect.ClassTag
import scala.collection.generic.CanBuildFrom
import scala.collection.mutable.Builder
import scala.collection.{immutable => i, mutable => m}

/* builder optimized for a single ++= call, which returns identity on result if possible
* and defers to the underlying builder if not.
*/
private final class IdentityPreservingBuilder[A, CC[X] <: TraversableOnce[X]](that: Builder[A, CC[A]])(implicit ct: ClassTag[CC[A]])
extends Builder[A, CC[A]] {
var collection: CC[A] = null.asInstanceOf[CC[A]]
var ruined = false

final override def ++=(elems: TraversableOnce[A]): this.type =
elems match {
case ct(ca) if (collection == null && !ruined) => {
collection = ca
this
}
case _ => {
ruined = true
if (collection != null) that ++= collection
that ++= elems
collection = null.asInstanceOf[CC[A]]
this
}
}

final def +=(elem: A): this.type = {
collection = null.asInstanceOf[CC[A]]
ruined = true
that += elem
this
}
final def clear(): Unit = {
collection = null.asInstanceOf[CC[A]]
if (ruined) that.clear()
}
final def result(): CC[A] = if(ruined || (collection == null)) that.result() else collection
}

private[compat] object CompatImpl {
def simpleCBF[A, C](f: => Builder[A, C]): CanBuildFrom[Any, A, C] = new CanBuildFrom[Any, A, C] {
def apply(from: Any): Builder[A, C] = apply()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ package scala.collection.compat

import scala.collection.generic._
import scala.reflect.ClassTag
import scala.collection.{MapLike, GenTraversable, BitSet, IterableView}
import scala.collection.{LinearSeq, MapLike, GenTraversable, BitSet, IterableView}
import scala.collection.{immutable => i, mutable => m}
import scala.{collection => c}

Expand Down Expand Up @@ -45,8 +45,12 @@ private[compat] trait PackageShared {
}

implicit def genericCompanionToCBF[A, CC[X] <: GenTraversable[X]](
fact: GenericCompanion[CC]): CanBuildFrom[Any, A, CC[A]] =
simpleCBF(fact.newBuilder[A])
fact: GenericCompanion[CC]): CanBuildFrom[Any, A, CC[A]] = {
val builder = if (fact == Seq) new IdentityPreservingBuilder[A, Seq](Seq.newBuilder[A])
else if (fact == LinearSeq) new IdentityPreservingBuilder[A, LinearSeq](LinearSeq.newBuilder[A])
else fact.newBuilder[A]
simpleCBF(builder.asInstanceOf[m.Builder[A, CC[A]]])
}

implicit def sortedSetCompanionToCBF[A: Ordering,
CC[X] <: c.SortedSet[X] with c.SortedSetLike[X, CC[X]]](
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import org.junit.Test

import scala.collection.compat._
import scala.collection.immutable.BitSet
import scala.collection.LinearSeq

class CollectionTest {
@Test
Expand All @@ -40,6 +41,11 @@ class CollectionTest {
//val mT: Map[Int, String] = m
assertEquals(Map(1 -> "a", 2 -> "b"), m)
assertTrue(m.isInstanceOf[Map[_, _]])

// Stream.to(Seq) doesn't evaluate the stream
val strm = 1 #:: {throw new Exception("not lazy")} #:: Stream.empty[Int]
val strmsq: Seq[Int] = strm.to(Seq)
var strmln: LinearSeq[Int] = strm.to(LinearSeq)
}

@Test
Expand Down