Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion library/src/scala/IArray.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import reflect.ClassTag

import language.experimental.captureChecking

import scala.collection.{LazyZip2, SeqView, Searching, Stepper, StepperShape}
import scala.annotation.experimental

import scala.collection.{LazyZip2, SeqView, Searching, Stepper, StepperShape, Factory}
import scala.collection.immutable.ArraySeq
import scala.collection.mutable.{ArrayBuilder, Builder}

Expand All @@ -14,6 +16,19 @@ opaque type IArray[+T] = Array[? <: T]
*/
object IArray:

// OLD style implicit conversion so no import is needed at use-site.
// once `into` feature is made stable, we could make `Factory` an `into` trait, and
// change this to the new style `given Conversion`.
/** Provides an implicit conversion from the IArray object to a collection Factory */
@experimental
implicit def convertIArrayToFactory[A : ClassTag](@annotation.unused asFactory: IArray.type): Factory[A, IArray[A]] =
// copied from ArrayFactory, i guess its possible to capture a factory?
@SerialVersionUID(3L)
class ConcreteIArrayFactory extends Factory[A, IArray[A]] with Serializable:
def fromSpecific(it: IterableOnce[A]^): IArray[A] = IArray.from[A](it)
def newBuilder: Builder[A, IArray[A]] = IArray.newBuilder[A]
ConcreteIArrayFactory()

/** The selection operation on an immutable array.
*
* @param arr the immutable array
Expand Down
3 changes: 3 additions & 0 deletions tests/run-tasty-inspector/stdlibExperimentalDefinitions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ val experimentalDefinitionInLibrary = Set(

// New feature: Erased trait
"scala.compiletime.Erased",

// New API: IArray Factory conversion
"scala.IArray$package$.IArray$.convertIArrayToFactory",
)


Expand Down
16 changes: 16 additions & 0 deletions tests/run/IArrayOps-experimental-Factory.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//> using options -experimental

import scala.collection.Factory

@main def Test: Unit =
val explicit: Factory[Int, IArray[Int]] = IArray.convertIArrayToFactory[Int](IArray)
val contextual: Factory[Int, IArray[Int]] = IArray: Factory[Int, IArray[Int]]

assert(explicit.getClass() == contextual.getClass()) // default is same implementation

val convertFromList = List(1,2,3).to(IArray)
val ev: IArray[Int] = convertFromList

assert(convertFromList.getClass == IArray(1,2,3).getClass())

assert(ev.sameElements(Vector(1,2,3)))
Loading