Skip to content

Commit

Permalink
Avoid using ArraySeq.empty with fixed element type
Browse files Browse the repository at this point in the history
  • Loading branch information
som-snytt committed May 25, 2021
1 parent 3b1b2d1 commit 8fa1580
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/library/scala/collection/StrictOptimizedSeqOps.scala
Expand Up @@ -94,7 +94,7 @@ trait StrictOptimizedSeqOps [+A, +CC[_], +C]
}

override def intersect[B >: A](that: Seq[B]): C =
if (isEmpty || that.isEmpty) empty
if (isEmpty || that.isEmpty) newSpecificBuilder.result()
else {
val occ = occCounts(that)
val b = newSpecificBuilder
Expand Down
22 changes: 21 additions & 1 deletion test/junit/scala/collection/StrictOptimizedSeqTest.scala
@@ -1,6 +1,6 @@
package scala.collection

import org.junit.Assert.assertEquals
import org.junit.Assert.{assertEquals, assertTrue}
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
Expand All @@ -20,4 +20,24 @@ class StrictOptimizedSeqTest {

assertEquals(Vector("a", "aa", "aaa", "bbbb"), result)
}

@Test
def `empty intersection has correct component type for array`(): Unit = {
val something = Array(3.14)
val nothing = Array[Double]()
val empty = Array.empty[Double]

assertEquals(classOf[Double], nothing.intersect(something).getClass.getComponentType)
assertTrue(nothing.intersect(something).isEmpty)

assertEquals(classOf[Double], empty.intersect(something).getClass.getComponentType)
assertTrue(empty.intersect(something).isEmpty)
assertEquals(classOf[Double], empty.intersect(nothing).getClass.getComponentType)
assertTrue(empty.intersect(nothing).isEmpty)

assertEquals(classOf[Double], something.intersect(nothing).getClass.getComponentType)
assertTrue(something.intersect(nothing).isEmpty)
assertEquals(classOf[Double], something.intersect(empty).getClass.getComponentType)
assertTrue(something.intersect(empty).isEmpty)
}
}

0 comments on commit 8fa1580

Please sign in to comment.