Skip to content

Commit b94bf42

Browse files
committed
Emit StrictFactory for arrays and strings
Specifically for Scala.js, which the compiler hasn't yet known that Array is pure.
1 parent 7d99e9f commit b94bf42

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

library/src/scala/collection/Factory.scala

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,16 @@ trait Factory[-A, +C] extends Any { self =>
4646
def newBuilder: Builder[A, C]
4747
}
4848

49+
/** A strict version of [[Factory]], where the returned collection from [[fromSpecific]] do not capture its input. */
50+
trait StrictFactory[-A, +C] extends Factory[A, C] {
51+
override def fromSpecific(it: IterableOnce[A]^): C
52+
}
53+
4954
object Factory {
5055

51-
implicit val stringFactory: Factory[Char, String] = new StringFactory
56+
implicit val stringFactory: StrictFactory[Char, String] = new StringFactory
5257
@SerialVersionUID(3L)
53-
private class StringFactory extends Factory[Char, String] with Serializable {
58+
private class StringFactory extends StrictFactory[Char, String] with Serializable {
5459
def fromSpecific(it: IterableOnce[Char]^): String = {
5560
val b = new mutable.StringBuilder(scala.math.max(0, it.knownSize))
5661
b ++= it
@@ -59,9 +64,9 @@ object Factory {
5964
def newBuilder: Builder[Char, String] = new mutable.StringBuilder()
6065
}
6166

62-
implicit def arrayFactory[A: ClassTag]: Factory[A, Array[A]] = new ArrayFactory[A]
67+
implicit def arrayFactory[A: ClassTag]: StrictFactory[A, Array[A]] = new ArrayFactory[A]
6368
@SerialVersionUID(3L)
64-
private class ArrayFactory[A: ClassTag] extends Factory[A, Array[A]] with Serializable {
69+
private class ArrayFactory[A: ClassTag] extends StrictFactory[A, Array[A]] with Serializable {
6570
def fromSpecific(it: IterableOnce[A]^): Array[A] = {
6671
val b = newBuilder
6772
b.sizeHint(it, delta = 0)

0 commit comments

Comments
 (0)