Skip to content

Commit c828122

Browse files
authored
Merge pull request #338 from oxbowlakes/issue-337-factory
Issue 337 factory.newBuilder should return new instances
2 parents 9e18341 + c4a16a7 commit c828122

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

.scalafmt.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
version=1.5.1
12
align = more
23
docstrings = JavaDoc
34
assumeStandardLibraryStripMargin = true

compat/src/main/scala-2.11_2.12/scala/collection/compat/PackageShared.scala

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,12 @@ private[compat] trait PackageShared {
5555

5656
implicit def genericCompanionToCBF[A, CC[X] <: GenTraversable[X]](
5757
fact: GenericCompanion[CC]): CanBuildFrom[Any, A, CC[A]] = {
58-
val builder: m.Builder[A, CC[A]] = fact match {
58+
/* see https://github.com/scala/scala-library-compat/issues/337
59+
`simpleCBF.apply` takes a by-name parameter and relies on
60+
repeated references generating new builders, thus this expression
61+
must be non-strict
62+
*/
63+
def builder: m.Builder[A, CC[A]] = fact match {
5964
case c.Seq | i.Seq => new IdentityPreservingBuilder[A, i.Seq](i.Seq.newBuilder[A])
6065
case c.LinearSeq | i.LinearSeq =>
6166
new IdentityPreservingBuilder[A, i.LinearSeq](i.LinearSeq.newBuilder[A])

compat/src/test/scala/test/scala/collection/FactoryTest.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,19 @@ class FactoryTest {
4242
val result = factory.fromSpecific(source)
4343
Assert.assertEquals(1, counter) // One element has been evaluated because Stream is not lazy in its head
4444
}
45+
@Test
46+
def factoriesAreReusable(): Unit = {
47+
def generically[M[X] <: Iterable[X]](in: M[Int], factory: Factory[Int, M[Int]]): Unit = {
48+
val l = Iterator(-3, -2, -1).to(factory)
49+
val m = in.iterator.to(factory)
50+
Assert.assertEquals(in, m)
51+
}
52+
53+
generically[List](List(1, 2, 3), List)
54+
generically[Seq](Seq(1, 2, 3), Seq)
55+
generically[IndexedSeq](IndexedSeq(1, 2, 3), IndexedSeq)
56+
generically[Vector](Vector(1, 2, 3), Vector)
57+
generically[Set](Set(1, 2, 3), Set)
58+
}
4559

4660
}

0 commit comments

Comments
 (0)