Skip to content

Commit 836c5a9

Browse files
committed
ArrayOps must convert to result array type
1 parent 94e5fe3 commit 836c5a9

File tree

3 files changed

+42
-13
lines changed

3 files changed

+42
-13
lines changed

src/library/scala/collection/ArrayOps.scala

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1569,18 +1569,18 @@ final class ArrayOps[A](private val xs: Array[A]) extends AnyVal {
15691569
* ''n'' times in `that`, then the first ''n'' occurrences of `x` will not form
15701570
* part of the result, but any following occurrences will.
15711571
*/
1572-
def diff[B >: A](that: Seq[B]): Array[A] = mutable.ArraySeq.make(xs).diff(that).array.asInstanceOf[Array[A]]
1572+
def diff[B >: A](that: Seq[B]): Array[A] = mutable.ArraySeq.make(xs).diff(that).toArray[A]
15731573

15741574
/** Computes the multiset intersection between this array and another sequence.
1575-
*
1576-
* @param that the sequence of elements to intersect with.
1577-
* @return a new array which contains all elements of this array
1578-
* which also appear in `that`.
1579-
* If an element value `x` appears
1580-
* ''n'' times in `that`, then the first ''n'' occurrences of `x` will be retained
1581-
* in the result, but any following occurrences will be omitted.
1582-
*/
1583-
def intersect[B >: A](that: Seq[B]): Array[A] = mutable.ArraySeq.make(xs).intersect(that).array.asInstanceOf[Array[A]]
1575+
*
1576+
* @param that the sequence of elements to intersect with.
1577+
* @return a new array which contains all elements of this array
1578+
* which also appear in `that`.
1579+
* If an element value `x` appears
1580+
* ''n'' times in `that`, then the first ''n'' occurrences of `x` will be retained
1581+
* in the result, but any following occurrences will be omitted.
1582+
*/
1583+
def intersect[B >: A](that: Seq[B]): Array[A] = mutable.ArraySeq.make(xs).intersect(that).toArray[A]
15841584

15851585
/** Groups elements in fixed size blocks by passing a "sliding window"
15861586
* over them (as opposed to partitioning them, as is done in grouped.)
@@ -1592,7 +1592,7 @@ final class ArrayOps[A](private val xs: Array[A]) extends AnyVal {
15921592
* last element (which may be the only element) will be truncated
15931593
* if there are fewer than `size` elements remaining to be grouped.
15941594
*/
1595-
def sliding(size: Int, step: Int = 1): Iterator[Array[A]] = mutable.ArraySeq.make(xs).sliding(size, step).map(_.array.asInstanceOf[Array[A]])
1595+
def sliding(size: Int, step: Int = 1): Iterator[Array[A]] = mutable.ArraySeq.make(xs).sliding(size, step).map(_.toArray[A])
15961596

15971597
/** Iterates over combinations. A _combination_ of length `n` is a subsequence of
15981598
* the original array, with the elements taken in order. Thus, `Array("x", "y")` and `Array("y", "y")`
@@ -1609,7 +1609,7 @@ final class ArrayOps[A](private val xs: Array[A]) extends AnyVal {
16091609
* Array("a", "b", "b", "b", "c").combinations(2) == Iterator(Array(a, b), Array(a, c), Array(b, b), Array(b, c))
16101610
* }}}
16111611
*/
1612-
def combinations(n: Int): Iterator[Array[A]] = mutable.ArraySeq.make(xs).combinations(n).map(_.array.asInstanceOf[Array[A]])
1612+
def combinations(n: Int): Iterator[Array[A]] = mutable.ArraySeq.make(xs).combinations(n).map(_.toArray[A])
16131613

16141614
/** Iterates over distinct permutations.
16151615
*
@@ -1618,7 +1618,7 @@ final class ArrayOps[A](private val xs: Array[A]) extends AnyVal {
16181618
* Array("a", "b", "b").permutations == Iterator(Array(a, b, b), Array(b, a, b), Array(b, b, a))
16191619
* }}}
16201620
*/
1621-
def permutations: Iterator[Array[A]] = mutable.ArraySeq.make(xs).permutations.map(_.array.asInstanceOf[Array[A]])
1621+
def permutations: Iterator[Array[A]] = mutable.ArraySeq.make(xs).permutations.map(_.toArray[A])
16221622

16231623
// we have another overload here, so we need to duplicate this method
16241624
/** Tests whether this array contains the given sequence at a given index.

test/files/run/t12403.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
object Test extends App {
3+
val xs =
4+
Array.empty[Double]
5+
val ys =
6+
Array(0.0)
7+
assert(xs.intersect(ys).getClass.getComponentType == classOf[Double])
8+
assert(Array.empty[Double].intersect(Array(0.0)).getClass.getComponentType == classOf[Double])
9+
}

test/junit/scala/collection/ArrayOpsTest.scala

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,24 @@ class ArrayOpsTest {
122122
val a: Array[Byte] = new Array[Byte](1000).sortWith { _ < _ }
123123
assertEquals(0, a(0))
124124
}
125+
126+
@Test
127+
def `empty intersection has correct component type for array`(): Unit = {
128+
val something = Array(3.14)
129+
val nothing = Array[Double]()
130+
val empty = Array.empty[Double]
131+
132+
assertEquals(classOf[Double], nothing.intersect(something).getClass.getComponentType)
133+
assertTrue(nothing.intersect(something).isEmpty)
134+
135+
assertEquals(classOf[Double], empty.intersect(something).getClass.getComponentType)
136+
assertTrue(empty.intersect(something).isEmpty)
137+
assertEquals(classOf[Double], empty.intersect(nothing).getClass.getComponentType)
138+
assertTrue(empty.intersect(nothing).isEmpty)
139+
140+
assertEquals(classOf[Double], something.intersect(nothing).getClass.getComponentType)
141+
assertTrue(something.intersect(nothing).isEmpty)
142+
assertEquals(classOf[Double], something.intersect(empty).getClass.getComponentType)
143+
assertTrue(something.intersect(empty).isEmpty)
144+
}
125145
}

0 commit comments

Comments
 (0)