Skip to content

Commit 6afa36a

Browse files
committed
Add Array.from extension for scala 2.11 and 2.12
1 parent 3f5e22e commit 6afa36a

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,9 @@ private[compat] trait PackageShared {
272272
builder.result()
273273
}
274274

275+
implicit def toArrayExtensions(fact: Array.type): ArrayExtensions =
276+
new ArrayExtensions(fact)
277+
275278
implicit def toImmutableSortedMapExtensions(
276279
fact: i.SortedMap.type): ImmutableSortedMapExtensions =
277280
new ImmutableSortedMapExtensions(fact)
@@ -357,6 +360,11 @@ private[compat] trait PackageShared {
357360
new RandomExtensions(self)
358361
}
359362

363+
class ArrayExtensions(private val fact: Array.type) extends AnyVal {
364+
def from[A: ClassTag](source: TraversableOnce[A]): Array[A] =
365+
fact.apply(source.toSeq: _*)
366+
}
367+
360368
class ImmutableSortedMapExtensions(private val fact: i.SortedMap.type) extends AnyVal {
361369
def from[K: Ordering, V](source: TraversableOnce[(K, V)]): i.SortedMap[K, V] =
362370
build(i.SortedMap.newBuilder[K, V], source)

compat/src/test/scala/test/scala/collection/CollectionTest.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ package test.scala.collection
1515
import org.junit.Assert._
1616
import org.junit.Test
1717

18+
import java.util
1819
import scala.collection.compat._
1920
import scala.collection.immutable.BitSet
2021
import scala.collection.mutable.PriorityQueue
@@ -56,6 +57,9 @@ class CollectionTest {
5657
@Test
5758
def testFrom: Unit = {
5859
val xs = List(1, 2, 3)
60+
val a = Array.from(xs)
61+
val aT: Array[Int] = a
62+
assertTrue(Array(1, 2, 3).sameElements(a))
5963
val v = Vector.from(xs)
6064
val vT: Vector[Int] = v
6165
assertEquals(Vector(1, 2, 3), v)

0 commit comments

Comments
 (0)