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 5aa45f83..a5511615 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 @@ -272,6 +272,9 @@ private[compat] trait PackageShared { builder.result() } + implicit def toArrayExtensions(fact: Array.type): ArrayExtensions = + new ArrayExtensions(fact) + implicit def toImmutableSortedMapExtensions( fact: i.SortedMap.type): ImmutableSortedMapExtensions = new ImmutableSortedMapExtensions(fact) @@ -357,6 +360,14 @@ private[compat] trait PackageShared { new RandomExtensions(self) } +final class ArrayExtensions(private val fact: Array.type) extends AnyVal { + def from[A: ClassTag](source: TraversableOnce[A]): Array[A] = + source match { + case it: Iterable[A] => it.toArray[A] + case _ => source.toIterator.toArray[A] + } +} + class ImmutableSortedMapExtensions(private val fact: i.SortedMap.type) extends AnyVal { def from[K: Ordering, V](source: TraversableOnce[(K, V)]): i.SortedMap[K, V] = build(i.SortedMap.newBuilder[K, V], source) diff --git a/compat/src/test/scala/test/scala/collection/CollectionTest.scala b/compat/src/test/scala/test/scala/collection/CollectionTest.scala index 2bd6955b..2902db91 100644 --- a/compat/src/test/scala/test/scala/collection/CollectionTest.scala +++ b/compat/src/test/scala/test/scala/collection/CollectionTest.scala @@ -56,6 +56,9 @@ class CollectionTest { @Test def testFrom: Unit = { val xs = List(1, 2, 3) + val a = Array.from(xs) + val aT: Array[Int] = a + assertTrue(Array(1, 2, 3).sameElements(a)) val v = Vector.from(xs) val vT: Vector[Int] = v assertEquals(Vector(1, 2, 3), v)