Skip to content

Commit dc81415

Browse files
committed
Specialize liftable for IArrays
1 parent 232b3fa commit dc81415

File tree

4 files changed

+102
-5
lines changed

4 files changed

+102
-5
lines changed

library/src/scala/quoted/Liftable.scala

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,63 @@ object Liftable {
4545
}
4646
}
4747

48-
given [T: Type: Liftable: ClassTag] as Liftable[IArray[T]] = new Liftable[IArray[T]] {
49-
def toExpr(iarray: IArray[T]): given QuoteContext => Expr[IArray[T]] = '{
50-
val array = new Array[T](${iarray.length.toExpr})(ClassTag(${the[ClassTag[T]].runtimeClass.toExpr}))
51-
${ Expr.block(List.tabulate(iarray.length)(i => '{ array(${i.toExpr}) = ${iarray(i).toExpr} }), '{ array.asInstanceOf[IArray[T]] }) }
52-
}
48+
given [T: Type] as Liftable[ClassTag[T]] = new Liftable[ClassTag[T]] {
49+
def toExpr(x: ClassTag[T]): given QuoteContext => Expr[ClassTag[T]] =
50+
'{ ClassTag(${x.runtimeClass.toExpr}) }
51+
}
52+
53+
given IArrayIsLiftable [T: Type: Liftable: ClassTag] as Liftable[IArray[T]] = new Liftable[IArray[T]] {
54+
def toExpr(iarray: IArray[T]): given QuoteContext => Expr[IArray[T]] =
55+
if (iarray.length == 0) '{ IArray.empty[T](${the[ClassTag[T]].toExpr}) }
56+
else '{ IArray(${iarray.asInstanceOf[Array[T]].toSeq.toExpr}: _*)(${the[ClassTag[T]].toExpr}) }
57+
}
58+
59+
given IArrayOfBooleanIsLiftable as Liftable[IArray[Boolean]] = new Liftable[IArray[Boolean]] {
60+
def toExpr(iarray: IArray[Boolean]): given QuoteContext => Expr[IArray[Boolean]] =
61+
if (iarray.length == 0) '{ Array.emptyBooleanArray.asInstanceOf[IArray[Boolean]] } // TODO use IArray.emptyBooleanIArray
62+
else '{ IArray(${iarray(0).toExpr}, ${iarray.asInstanceOf[Array[Boolean]].toSeq.tail.toExpr}: _*) } // TODO use IArray.toSeq
63+
}
64+
65+
given IArrayOfByteIsLiftable as Liftable[IArray[Byte]] = new Liftable[IArray[Byte]] {
66+
def toExpr(iarray: IArray[Byte]): given QuoteContext => Expr[IArray[Byte]] =
67+
if (iarray.length == 0) '{ Array.emptyByteArray.asInstanceOf[IArray[Byte]] } // TODO use IArray.emptyByteIArray
68+
else '{ IArray(${iarray(0).toExpr}, ${iarray.asInstanceOf[Array[Byte]].toSeq.tail.toExpr}: _*) } // TODO use IArray.toSeq
69+
}
70+
71+
given IArrayOfShortIsLiftable as Liftable[IArray[Short]] = new Liftable[IArray[Short]] {
72+
def toExpr(iarray: IArray[Short]): given QuoteContext => Expr[IArray[Short]] =
73+
if (iarray.length == 0) '{ Array.emptyShortArray.asInstanceOf[IArray[Short]] } // TODO use IArray.emptyShortIArray
74+
else '{ IArray(${iarray(0).toExpr}, ${iarray.asInstanceOf[Array[Short]].toSeq.tail.toExpr}: _*) } // TODO use IArray.toSeq
75+
}
76+
77+
given IArrayOfCharIsLiftable as Liftable[IArray[Char]] = new Liftable[IArray[Char]] {
78+
def toExpr(iarray: IArray[Char]): given QuoteContext => Expr[IArray[Char]] =
79+
if (iarray.length == 0) '{ Array.emptyCharArray.asInstanceOf[IArray[Char]] } // TODO use IArray.emptyCharIArray
80+
else '{ IArray(${iarray(0).toExpr}, ${iarray.asInstanceOf[Array[Char]].toSeq.tail.toExpr}: _*) } // TODO use IArray.toSeq
81+
}
82+
83+
given IArrayOfIntIsLiftable as Liftable[IArray[Int]] = new Liftable[IArray[Int]] {
84+
def toExpr(iarray: IArray[Int]): given QuoteContext => Expr[IArray[Int]] =
85+
if (iarray.length == 0) '{ Array.emptyIntArray.asInstanceOf[IArray[Int]] } // TODO use IArray.emptyIntIArray
86+
else '{ IArray(${iarray(0).toExpr}, ${iarray.asInstanceOf[Array[Int]].toSeq.tail.toExpr}: _*) } // TODO use IArray.toSeq
87+
}
88+
89+
given IArrayOfLongIsLiftable as Liftable[IArray[Long]] = new Liftable[IArray[Long]] {
90+
def toExpr(iarray: IArray[Long]): given QuoteContext => Expr[IArray[Long]] =
91+
if (iarray.length == 0) '{ Array.emptyLongArray.asInstanceOf[IArray[Long]] } // TODO use IArray.emptyLongIArray
92+
else '{ IArray(${iarray(0).toExpr}, ${iarray.asInstanceOf[Array[Long]].toSeq.tail.toExpr}: _*) } // TODO use IArray.toSeq
93+
}
94+
95+
given IArrayOfFloatIsLiftable as Liftable[IArray[Float]] = new Liftable[IArray[Float]] {
96+
def toExpr(iarray: IArray[Float]): given QuoteContext => Expr[IArray[Float]] =
97+
if (iarray.length == 0) '{ Array.emptyFloatArray.asInstanceOf[IArray[Float]] } // TODO use IArray.emptyFloatIArray
98+
else '{ IArray(${iarray(0).toExpr}, ${iarray.asInstanceOf[Array[Float]].toSeq.tail.toExpr}: _*) } // TODO use IArray.toSeq
99+
}
100+
101+
given IArrayOfDoubleIsLiftable as Liftable[IArray[Double]] = new Liftable[IArray[Double]] {
102+
def toExpr(iarray: IArray[Double]): given QuoteContext => Expr[IArray[Double]] =
103+
if (iarray.length == 0) '{ Array.emptyDoubleArray.asInstanceOf[IArray[Double]] } // TODO use IArray.emptyDoubleIArray
104+
else '{ IArray(${iarray(0).toExpr}, ${iarray.asInstanceOf[Array[Double]].toSeq.tail.toExpr}: _*) } // TODO use IArray.toSeq
53105
}
54106

55107
given [T: Type: Liftable] as Liftable[Seq[T]] = new Liftable[Seq[T]] {

tests/run-with-compiler/quote-lib.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,19 @@ object Test {
6868
val iarray: IArray[Int] = IArray(1, 2, 3)
6969
val liftedIArray: Expr[IArray[Int]] = iarray
7070

71+
val iarray2: IArray[String] = IArray("a", "b", "c")
72+
iarray2.toExpr
73+
74+
IArray(false).toExpr
75+
IArray(1: Byte).toExpr
76+
IArray(1: Short).toExpr
77+
IArray(1).toExpr
78+
IArray(1L).toExpr
79+
IArray(1.1f).toExpr
80+
IArray(1.1d).toExpr
81+
IArray('a').toExpr
82+
IArray((1, 3)).toExpr
83+
7184
val some: Option[Int] = Some(2)
7285
val none: Option[Int] = Some(2)
7386
val liftedSome: Expr[Option[Int]] = some

tests/run-with-compiler/quote-lift-IArray.check

Whitespace-only changes.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import scala.quoted._
2+
import scala.quoted.autolift._
3+
4+
object Test {
5+
implicit val toolbox: scala.quoted.Toolbox = scala.quoted.Toolbox.make(getClass.getClassLoader)
6+
def main(args: Array[String]): Unit = run {
7+
'{
8+
def p[T](arr: IArray[T]): Unit = {
9+
println(arr.asInstanceOf[Array[_]].mkString("[", ", ", "]"))
10+
}
11+
p(${IArray.empty[Boolean]})
12+
p(${IArray.empty[Byte]})
13+
p(${IArray.empty[Short]})
14+
p(${IArray.empty[Char]})
15+
p(${IArray.empty[Int]})
16+
p(${IArray.empty[Long]})
17+
p(${IArray.empty[Float]})
18+
p(${IArray.empty[Double]})
19+
p(${IArray.empty[String]})
20+
println()
21+
p(${IArray(true)})
22+
p(${IArray[Byte](1, 2)})
23+
p(${IArray[Short](2, 3)})
24+
p(${IArray[Char]('a', 'b')})
25+
p(${IArray[Int](4, 5)})
26+
p(${IArray[Long](6L, 7L)})
27+
p(${IArray[Float](2.1f, 3.2f)})
28+
p(${IArray[Double](2.2, 3.3)})
29+
p(${IArray("abc", "xyz")})
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)