From 7872b6ab446b1562df5dc7e22ee3684d3cf4a3ac Mon Sep 17 00:00:00 2001 From: Christopher Marshall Date: Fri, 29 May 2020 13:26:49 +0100 Subject: [PATCH 1/4] Adds test that factories are reusable --- .../test/scala/collection/FactoryTest.scala | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/compat/src/test/scala/test/scala/collection/FactoryTest.scala b/compat/src/test/scala/test/scala/collection/FactoryTest.scala index 0f01f097..b96b76da 100644 --- a/compat/src/test/scala/test/scala/collection/FactoryTest.scala +++ b/compat/src/test/scala/test/scala/collection/FactoryTest.scala @@ -43,4 +43,20 @@ class FactoryTest { Assert.assertEquals(1, counter) // One element has been evaluated because Stream is not lazy in its head } + + @Test + def factoriesAreReusable(): Unit = { + def generically[M[X] <: Iterable[X]](in: M[Int], factory: Factory[Int, M[Int]]): Unit = { + val l = Iterator(-3, -2, -1).to(factory) + val m = in.iterator.to(factory) + Assert.assertEquals(in, m) + } + + generically[List](List(1, 2, 3), List) + generically[Seq](Seq(1, 2, 3), Seq) + generically[IndexedSeq](IndexedSeq(1, 2, 3), IndexedSeq) + generically[Vector](Vector(1, 2, 3), Vector) + generically[Set](Set(1, 2, 3), Set) + } + } From f481be924eb1f26b67b1e286f7f4ca36aed925b8 Mon Sep 17 00:00:00 2001 From: Christopher Marshall Date: Fri, 29 May 2020 13:31:02 +0100 Subject: [PATCH 2/4] Fixes Issue #337 by making builder expression non-strict - See https://github.com/scala/scala-library-compat/issues/337 - --- .../scala/collection/compat/PackageShared.scala | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/compat/src/main/scala-2.11_2.12/scala/collection/compat/PackageShared.scala b/compat/src/main/scala-2.11_2.12/scala/collection/compat/PackageShared.scala index 9962cb3f..06ae19b7 100644 --- a/compat/src/main/scala-2.11_2.12/scala/collection/compat/PackageShared.scala +++ b/compat/src/main/scala-2.11_2.12/scala/collection/compat/PackageShared.scala @@ -55,7 +55,12 @@ private[compat] trait PackageShared { implicit def genericCompanionToCBF[A, CC[X] <: GenTraversable[X]]( fact: GenericCompanion[CC]): CanBuildFrom[Any, A, CC[A]] = { - val builder: m.Builder[A, CC[A]] = fact match { + /* see https://github.com/scala/scala-library-compat/issues/337 + `simpleCBF.apply` takes a by-name parameter and relies on + repeated references generating new builders, thus this expression + must be non-strict + */ + def builder: m.Builder[A, CC[A]] = fact match { case c.Seq | i.Seq => new IdentityPreservingBuilder[A, i.Seq](i.Seq.newBuilder[A]) case c.LinearSeq | i.LinearSeq => new IdentityPreservingBuilder[A, i.LinearSeq](i.LinearSeq.newBuilder[A]) From 3336da5ff87d7805481b0bac1cab4f3f345e9764 Mon Sep 17 00:00:00 2001 From: Julien Richard-Foy Date: Tue, 2 Jun 2020 09:44:46 +0200 Subject: [PATCH 3/4] Set scalafmt version in configuration file --- .scalafmt.conf | 1 + 1 file changed, 1 insertion(+) diff --git a/.scalafmt.conf b/.scalafmt.conf index 20a6dd5a..5ed5dd26 100644 --- a/.scalafmt.conf +++ b/.scalafmt.conf @@ -1,3 +1,4 @@ +version=1.5.1 align = more docstrings = JavaDoc assumeStandardLibraryStripMargin = true From c4a16a7bfa31b3dfdcb2b37e495e0c0dbbd1cabd Mon Sep 17 00:00:00 2001 From: Julien Richard-Foy Date: Tue, 2 Jun 2020 09:44:55 +0200 Subject: [PATCH 4/4] Apply scalafmt --- compat/src/test/scala/test/scala/collection/FactoryTest.scala | 2 -- 1 file changed, 2 deletions(-) diff --git a/compat/src/test/scala/test/scala/collection/FactoryTest.scala b/compat/src/test/scala/test/scala/collection/FactoryTest.scala index b96b76da..4186bef2 100644 --- a/compat/src/test/scala/test/scala/collection/FactoryTest.scala +++ b/compat/src/test/scala/test/scala/collection/FactoryTest.scala @@ -42,8 +42,6 @@ class FactoryTest { val result = factory.fromSpecific(source) Assert.assertEquals(1, counter) // One element has been evaluated because Stream is not lazy in its head } - - @Test def factoriesAreReusable(): Unit = { def generically[M[X] <: Iterable[X]](in: M[Int], factory: Factory[Int, M[Int]]): Unit = {