Skip to content

Commit 673694b

Browse files
committed
Implement Liftable explicitly
1 parent 86e5c12 commit 673694b

File tree

4 files changed

+79
-32
lines changed

4 files changed

+79
-32
lines changed

library/src/scala/quoted/Liftable.scala

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,45 @@ abstract class Liftable[T] {
1515
* gives an alternative implementation using just the basic staging system.
1616
*/
1717
object Liftable {
18-
implicit def BooleanIsLiftable: Liftable[Boolean] = (x: Boolean) => liftedExpr(x)
19-
implicit def ByteIsLiftable: Liftable[Byte] = (x: Byte) => liftedExpr(x)
20-
implicit def CharIsLiftable: Liftable[Char] = (x: Char) => liftedExpr(x)
21-
implicit def ShortIsLiftable: Liftable[Short] = (x: Short) => liftedExpr(x)
22-
implicit def IntIsLiftable: Liftable[Int] = (x: Int) => liftedExpr(x)
23-
implicit def LongIsLiftable: Liftable[Long] = (x: Long) => liftedExpr(x)
24-
implicit def FloatIsLiftable: Liftable[Float] = (x: Float) => liftedExpr(x)
25-
implicit def DoubleIsLiftable: Liftable[Double] = (x: Double) => liftedExpr(x)
26-
27-
implicit def StringIsLiftable: Liftable[String] = (x: String) => liftedExpr(x)
28-
29-
implicit def ClassIsLiftable[T]: Liftable[Class[T]] = (x: Class[T]) => liftedExpr(x)
18+
19+
implicit def BooleanIsLiftable: Liftable[Boolean] = new Liftable[Boolean] {
20+
def toExpr(x: Boolean): Expr[Boolean] = liftedExpr(x)
21+
}
22+
23+
implicit def ByteIsLiftable: Liftable[Byte] = new Liftable[Byte] {
24+
def toExpr(x: Byte): Expr[Byte] = liftedExpr(x)
25+
}
26+
27+
implicit def CharIsLiftable: Liftable[Char] = new Liftable[Char] {
28+
def toExpr(x: Char): Expr[Char] = liftedExpr(x)
29+
}
30+
31+
implicit def ShortIsLiftable: Liftable[Short] = new Liftable[Short] {
32+
def toExpr(x: Short): Expr[Short] = liftedExpr(x)
33+
}
34+
35+
implicit def IntIsLiftable: Liftable[Int] = new Liftable[Int] {
36+
def toExpr(x: Int): Expr[Int] = liftedExpr(x)
37+
}
38+
39+
implicit def LongIsLiftable: Liftable[Long] = new Liftable[Long] {
40+
def toExpr(x: Long): Expr[Long] = liftedExpr(x)
41+
}
42+
43+
implicit def FloatIsLiftable: Liftable[Float] = new Liftable[Float] {
44+
def toExpr(x: Float): Expr[Float] = liftedExpr(x)
45+
}
46+
47+
implicit def DoubleIsLiftable: Liftable[Double] = new Liftable[Double] {
48+
def toExpr(x: Double): Expr[Double] = liftedExpr(x)
49+
}
50+
51+
implicit def StringIsLiftable: Liftable[String] = new Liftable[String] {
52+
def toExpr(x: String): Expr[String] = liftedExpr(x)
53+
}
54+
55+
implicit def ClassIsLiftable[T]: Liftable[Class[T]] = new Liftable[Class[T]] {
56+
def toExpr(x: Class[T]): Expr[Class[T]] = liftedExpr(x)
57+
}
58+
3059
}

tests/run-with-compiler/i3847-b.scala

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@ import scala.quoted._
33
import scala.reflect.ClassTag
44

55
object Arrays {
6-
implicit def ArrayIsLiftable[T: Liftable](implicit t: Type[T]): Liftable[Array[List[T]]] = (arr: Array[List[T]]) => '{
7-
new Array[List[~t]](~arr.length.toExpr)
8-
// TODO add elements
6+
implicit def ArrayIsLiftable[T: Liftable](implicit t: Type[T]): Liftable[Array[List[T]]] = {
7+
new Liftable[Array[List[T]]] {
8+
def toExpr(arr: Array[List[T]]): Expr[Array[List[T]]] = '{
9+
new Array[List[~t]](~arr.length.toExpr)
10+
// TODO add elements
11+
}
12+
}
913
}
1014
}
1115

tests/run-with-compiler/i3847.scala

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@ import scala.quoted._
33
import scala.reflect.ClassTag
44

55
object Arrays {
6-
implicit def ArrayIsLiftable[T: Liftable](implicit t: Type[T], ct: Expr[ClassTag[T]]): Liftable[Array[T]] = (arr: Array[T]) => '{
7-
new Array[~t](~arr.length.toExpr)(~ct)
8-
// TODO add elements
6+
implicit def ArrayIsLiftable[T: Liftable](implicit t: Type[T], ct: Expr[ClassTag[T]]): Liftable[Array[T]] = {
7+
new Liftable[Array[T]] {
8+
def toExpr(arr: Array[T]): Expr[Array[T]] = '{
9+
new Array[~t](~arr.length.toExpr)(~ct)
10+
// TODO add elements
11+
}
12+
}
913
}
1014
}
1115

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

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ package liftable {
5353
}
5454

5555
object Units {
56-
implicit def UnitIsLiftable: Liftable[Unit] = _=> '{ () }
56+
implicit def UnitIsLiftable: Liftable[Unit] = new Liftable[Unit] {
57+
def toExpr(x: Unit): Expr[Unit] = '()
58+
}
5759
}
5860

5961
object Lets {
@@ -72,20 +74,26 @@ package liftable {
7274

7375
object Tuples {
7476

75-
implicit def Tuple1IsLiftable[T1: Liftable](implicit t1: Type[T1]): Liftable[Tuple1[T1]] = {
76-
case Tuple1(x1: T1) => '{ Tuple1[~t1](~x1.toExpr) }
77+
implicit def Tuple1IsLiftable[T1: Liftable](implicit t1: Type[T1]): Liftable[Tuple1[T1]] = new Liftable[Tuple1[T1]] {
78+
def toExpr(x: Tuple1[T1]): Expr[Tuple1[T1]] =
79+
'{ Tuple1[~t1](~x._1.toExpr) }
7780
}
7881

79-
implicit def Tuple2IsLiftable[T1: Liftable, T2: Liftable](implicit t1: Type[T1], t2: Type[T2]): Liftable[(T1, T2)] = {
80-
x => '{ Tuple2[~t1, ~t2](~x._1.toExpr, ~x._2.toExpr) }
82+
implicit def Tuple2IsLiftable[T1: Liftable, T2: Liftable](implicit t1: Type[T1], t2: Type[T2]): Liftable[(T1, T2)] = new Liftable[(T1, T2)] {
83+
def toExpr(x: (T1, T2)): Expr[(T1, T2)] =
84+
'{ Tuple2[~t1, ~t2](~x._1.toExpr, ~x._2.toExpr) }
85+
8186
}
8287

83-
implicit def Tuple3IsLiftable[T1: Liftable, T2: Liftable, T3: Liftable](implicit t1: Type[T1], t2: Type[T2], t3: Type[T3]): Liftable[(T1, T2, T3)] = {
84-
x => '{ Tuple3[~t1, ~t2, ~t3](~x._1.toExpr, ~x._2.toExpr, ~x._3.toExpr) }
88+
implicit def Tuple3IsLiftable[T1: Liftable, T2: Liftable, T3: Liftable](implicit t1: Type[T1], t2: Type[T2], t3: Type[T3]): Liftable[(T1, T2, T3)] = new Liftable[(T1, T2, T3)] {
89+
def toExpr(x: (T1, T2, T3)): Expr[(T1, T2, T3)] =
90+
'{ Tuple3[~t1, ~t2, ~t3](~x._1.toExpr, ~x._2.toExpr, ~x._3.toExpr) }
91+
8592
}
8693

87-
implicit def Tuple4IsLiftable[T1: Liftable, T2: Liftable, T3: Liftable, T4: Liftable](implicit t1: Type[T1], t2: Type[T2], t3: Type[T3], t4: Type[T4]): Liftable[(T1, T2, T3, T4)] = {
88-
x => '{ Tuple4[~t1, ~t2, ~t3, ~t4](~x._1.toExpr, ~x._2.toExpr, ~x._3.toExpr, ~x._4.toExpr) }
94+
implicit def Tuple4IsLiftable[T1: Liftable, T2: Liftable, T3: Liftable, T4: Liftable](implicit t1: Type[T1], t2: Type[T2], t3: Type[T3], t4: Type[T4]): Liftable[(T1, T2, T3, T4)] = new Liftable[(T1, T2, T3, T4)] {
95+
def toExpr(x: (T1, T2, T3, T4)): Expr[(T1, T2, T3, T4)] =
96+
'{ Tuple4[~t1, ~t2, ~t3, ~t4](~x._1.toExpr, ~x._2.toExpr, ~x._3.toExpr, ~x._4.toExpr) }
8997
}
9098

9199
// TODO more tuples
@@ -94,9 +102,11 @@ package liftable {
94102

95103

96104
object Lists {
97-
implicit def ListIsLiftable[T: Liftable](implicit t: Type[T]): Liftable[List[T]] = {
98-
case x :: xs => '{ (~xs.toExpr).::[~t](~x.toExpr) }
99-
case Nil => '{ Nil: List[~t] }
105+
implicit def ListIsLiftable[T: Liftable](implicit t: Type[T]): Liftable[List[T]] = new Liftable[List[T]] {
106+
def toExpr(x: List[T]): Expr[List[T]] = x match {
107+
case x :: xs => '{ (~xs.toExpr).::[~t](~x.toExpr) }
108+
case Nil => '{ Nil: List[~t] }
109+
}
100110
}
101111

102112
implicit class LiftedOps[T: Liftable](list: Expr[List[T]])(implicit t: Type[T]) {
@@ -118,8 +128,8 @@ package liftable {
118128
}
119129

120130
object Arrays {
121-
implicit def ArrayIsLiftable[T: Liftable](implicit t: Type[T], ct: Expr[ClassTag[T]]): Liftable[Array[T]] = (arr: Array[T]) => '{
122-
new Array[~t](~arr.length.toExpr)(~ct)
131+
implicit def ArrayIsLiftable[T: Liftable](implicit t: Type[T], ct: Expr[ClassTag[T]]): Liftable[Array[T]] = new Liftable[Array[T]] {
132+
def toExpr(arr: Array[T]): Expr[Array[T]] = '{ new Array[~t](~arr.length.toExpr)(~ct) }
123133
}
124134
}
125135

0 commit comments

Comments
 (0)