Skip to content

Commit bfc2eb6

Browse files
committed
Add implicit conversion from IArray.type to Factory
old style conversion so no language import is needed. When `into` is stabilized, then possibly `Factory` can be converted to an `into` trait, and the conversion turned into the new `given Conversion` style
1 parent d76d5a2 commit bfc2eb6

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

library/src/scala/IArray.scala

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import reflect.ClassTag
33

44
import language.experimental.captureChecking
55

6-
import scala.collection.{LazyZip2, SeqView, Searching, Stepper, StepperShape}
6+
import scala.annotation.experimental
7+
8+
import scala.collection.{LazyZip2, SeqView, Searching, Stepper, StepperShape, Factory}
79
import scala.collection.immutable.ArraySeq
810
import scala.collection.mutable.{ArrayBuilder, Builder}
911

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

19+
// OLD style implicit conversion so no import is needed at use-site.
20+
// once `into` feature is made stable, we could make `Factory` an `into` trait, and
21+
// change this to the new style `given Conversion`.
22+
/** Provides an implicit conversion from the IArray object to a collection Factory */
23+
@experimental
24+
implicit def convertIArrayToFactory[A : ClassTag](@annotation.unused asFactory: IArray.type): Factory[A, IArray[A]] =
25+
// copied from ArrayFactory, i guess its possible to capture a factory?
26+
@SerialVersionUID(3L)
27+
class ConcreteIArrayFactory extends Factory[A, IArray[A]] with Serializable:
28+
def fromSpecific(it: IterableOnce[A]^): IArray[A] = IArray.from[A](it)
29+
def newBuilder: Builder[A, IArray[A]] = IArray.newBuilder[A]
30+
ConcreteIArrayFactory()
31+
1732
/** The selection operation on an immutable array.
1833
*
1934
* @param arr the immutable array

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: IArray Factory conversion
103+
"scala.IArray$package$.IArray$.convertIArrayToFactory",
101104
)
102105

103106

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//> using options -experimental
2+
3+
import scala.collection.Factory
4+
5+
@main def Test: Unit =
6+
val explicit: Factory[Int, IArray[Int]] = IArray.convertIArrayToFactory[Int](IArray)
7+
val contextual: Factory[Int, IArray[Int]] = IArray: Factory[Int, IArray[Int]]
8+
9+
assert(explicit.getClass() == contextual.getClass()) // default is same implementation
10+
11+
val convertFromList = List(1,2,3).to(IArray)
12+
val ev: IArray[Int] = convertFromList
13+
14+
assert(convertFromList.getClass == IArray(1,2,3).getClass())
15+
16+
assert(ev.sameElements(Vector(1,2,3)))

0 commit comments

Comments
 (0)