Skip to content

Commit 7b6b127

Browse files
committed
Add canBuildFrom for arrays of value classes
1 parent 0651816 commit 7b6b127

File tree

4 files changed

+51
-1
lines changed

4 files changed

+51
-1
lines changed

src/dotty/DottyPredef.scala

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package dotty
22

3+
import scala.collection.generic.CanBuildFrom
34
import scala.reflect.runtime.universe.TypeTag
45
import scala.reflect.ClassTag
5-
import scala.collection.mutable.{ArrayOps, WrappedArray}
6+
import scala.collection.mutable.{ArrayBuilder, ArrayOps, WrappedArray}
67
import dotty.runtime.vc._
78
import scala.Predef.???
89

@@ -47,4 +48,19 @@ object DottyPredef {
4748
case x: Array[Unit] => scala.Predef.unitArrayOps(x)
4849
case null => null
4950
}).asInstanceOf[ArrayOps[T]]
51+
52+
implicit def canBuildFrom2[T](implicit t: ClassTag[T]): CanBuildFrom[Array[_], T, Array[T]] = {
53+
t match {
54+
case _: VCIntCompanion[_] | _: VCShortCompanion[_] |
55+
_: VCLongCompanion[_] | _: VCByteCompanion[_] |
56+
_: VCBooleanCompanion[_] | _: VCCharCompanion[_] |
57+
_: VCFloatCompanion[_] | _: VCDoubleCompanion[_] |
58+
_: VCObjectCompanion[_] =>
59+
new CanBuildFrom[Array[_], T, Array[T]] {
60+
def apply(from: Array[_]) = new VCArrayBuilder[T]()(t).asInstanceOf[ArrayBuilder[T]]
61+
def apply() = new VCArrayBuilder[T]()(t).asInstanceOf[ArrayBuilder[T]]
62+
}
63+
case _ => scala.Array.canBuildFrom[T](t)
64+
}
65+
}
5066
}

test/dotc/tests.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ class tests extends CompilerTest {
106106
@Test def pos_companions = compileFile(posDir, "companions", twice)
107107
@Test def pos_vc_proto_any_val = compileFile(vcArraysDir, "prototypeAnyVal", args = "-Ycheck:all" :: Nil)
108108
@Test def pos_vc_array_functions = compileFile(runDir, "valueclasses-array-functions", args = "-Ycheck:all" :: Nil)
109+
@Test def pos_vc_array_functions2 = compileFile(runDir, "valueclasses-array-functions2", args = "-Ycheck:all" :: Nil)
109110
@Test def pos_vc_array_usage = compileFile(runDir, "valueclasses-array-usage", args = "-Ycheck:all" :: Nil)
110111
@Test def pos_vc_array_gen = compileFile(runDir, "valueclasses-array-gen", args = "-Ycheck:all" :: Nil)
111112
@Test def pos_vc_array_int = compileFile(runDir, "valueclasses-array-int", args = "-Ycheck:all" :: Nil)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
r: Set(X(6), X(8), X(10))
2+
r3: Set(X(1), X(2), X(3))
3+
r4: Set(X(1), X(2), X(3))
4+
r5: Set(X(3), X(6), X(9))
5+
r6: Set(Z(1), Z(2), Z(3))
6+
v7: Set(Z(X(3)), Z(X(4)), Z(X(5)))
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class X(val x: Int) extends AnyVal {
2+
override def toString = s"X($x)"
3+
}
4+
class Y(val y: Int)
5+
class Z(val z: String) extends AnyVal {
6+
override def toString = s"Z($z)"
7+
}
8+
9+
object Test {
10+
def main(args: Array[String]) = {
11+
val r = test map { v1 => new X(v1.x*2) }
12+
println(s"r: ${r.toSet}")
13+
val r3 = Array(1,2,3).map(v3 => new X(v3))
14+
println(s"r3: ${r3.toSet}")
15+
val r4 = test2 map (v4 => new X(v4.y))
16+
println(s"r4: ${r4.toSet}")
17+
val r5 = test3 map {x => new X(x.asInstanceOf[X].x*3)}
18+
println(s"r5: ${r5.toSet}")
19+
val r6 = test2 map (v6 => new Z(v6.y.toString))
20+
println(s"r6: ${r6.toSet}")
21+
val r7 = test map (v7 => new Z(v7.toString))
22+
println(s"v7: ${r7.toSet}")
23+
}
24+
def test = Array(new X(3), new X(4), new X(5))
25+
def test2 = Array(new Y(1), new Y(2), new Y(3))
26+
def test3: Array[_] = Array(new X(1), new X(2), new X(3))
27+
}

0 commit comments

Comments
 (0)