Skip to content

Commit b69dd93

Browse files
committed
Add an implementation of IsSeq for IArray.
instead of just casting the Array variant, the underlying collection type should be immutable.ArraySeq.
1 parent fd2172d commit b69dd93

File tree

5 files changed

+44
-0
lines changed

5 files changed

+44
-0
lines changed

library/src/scala/collection/generic/IsSeq.scala

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import language.experimental.captureChecking
1818
import caps.unsafe.untrackedCaptures
1919

2020
import scala.reflect.ClassTag
21+
import scala.annotation.experimental
2122

2223
/** Type class witnessing that a collection representation type `Repr` has
2324
* elements of type `A` and has a conversion to `SeqOps[A, Iterable, C]`, for
@@ -106,6 +107,25 @@ object IsSeq {
106107
}
107108
}
108109

110+
@experimental
111+
given iarrayIsSeq[A0 : ClassTag]: (IsSeq[IArray[A0]] { type A = A0; type C = IArray[A0] }) =
112+
new IsSeq[IArray[A0]] {
113+
type A = A0
114+
type C = IArray[A0]
115+
def apply(a: IArray[A0]): SeqOps[A0, Seq, IArray[A0]] =
116+
new SeqOps[A, immutable.ArraySeq, IArray[A]] {
117+
def apply(i: Int): A = a(i)
118+
def length: Int = a.length
119+
def toIterable: Iterable[A] = IArray.genericWrapArray(a)
120+
protected def coll: IArray[A] = a
121+
protected def fromSpecific(coll: IterableOnce[A]^): IArray[A] = IArray.from(coll)
122+
def iterableFactory: IterableFactory[immutable.ArraySeq] = immutable.ArraySeq.untagged
123+
override def empty: IArray[A] = IArray.empty[A]
124+
protected def newSpecificBuilder: mutable.Builder[A, IArray[A]] = IArray.newBuilder
125+
def iterator: Iterator[A] = a.iterator
126+
}
127+
}
128+
109129
// `Range` can not be unified with the `CC0` parameter of the
110130
// `seqOpsIsSeq` definition because it does not take a type parameter.
111131
// Hence the need for a separate case:

project/MiMaFilters.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ object MiMaFilters {
2323
ProblemFilters.exclude[DirectMissingMethodProblem]("scala.NamedTuple.namedTupleOrdering"),
2424
ProblemFilters.exclude[MissingClassProblem]("scala.NamedTuple$namedTupleOrdering"),
2525

26+
// IArray integration with Scala Collections:
27+
ProblemFilters.exclude[DirectMissingMethodProblem]("scala.collection.generic.IsSeq.iarrayIsSeq"),
28+
2629
// cc related
2730
ProblemFilters.exclude[MissingClassProblem]("scala.annotation.internal.readOnlyCapability"),
2831
ProblemFilters.exclude[MissingClassProblem]("scala.annotation.internal.onlyCapability"),

tests/run-tasty-inspector/stdlibExperimentalDefinitions.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ val experimentalDefinitionInLibrary = Set(
9898

9999
// New feature: Erased trait
100100
"scala.compiletime.Erased",
101+
102+
// New API: IsSeq[IArray[T]]
103+
"scala.collection.generic.IsSeq$.iarrayIsSeq",
101104
)
102105

103106

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class scala.collection.immutable.ArraySeq$ofRef
2+
1,2
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//> using options -experimental
2+
3+
@main def Test: Unit =
4+
val isSeq1: scala.collection.generic.IsSeq[IArray[Int]] { type A = Int; type C = IArray[Int] } =
5+
scala.collection.generic.IsSeq.iarrayIsSeq[Int]
6+
val isSeq2: scala.collection.generic.IsSeq[IArray[Int]] { type A = Int; type C = IArray[Int] } =
7+
summon[scala.collection.generic.IsSeq[IArray[Int]]]
8+
assert(isSeq1.getClass eq isSeq2.getClass) // check that default implicit is same as explicit call
9+
10+
val iarr = IArray(1, 2, 3)
11+
val seqOps: scala.collection.SeqOps[Int, Iterable, IArray[Int]] = isSeq1(iarr)
12+
println(seqOps.map(_ * 2).getClass()) // should be immutable.ArraySeq.ofRef
13+
val evens: IArray[Int] = seqOps.filter(_ % 2 == 0)
14+
val buckets: Map[Boolean, IArray[Int]] = seqOps.groupBy(_ < 3)
15+
val smalls: IArray[Int] = buckets(true)
16+
println(smalls.mkString(","))

0 commit comments

Comments
 (0)