@@ -19,6 +19,8 @@ trait Liftable[T] {
19
19
*/
20
20
object Liftable {
21
21
22
+ // IMPORTANT Keep in sync with tests/run-staging/liftables.scala
23
+
22
24
given BooleanIsLiftable [T <: Boolean ] as Liftable [T ] = new PrimitiveLiftable
23
25
given ByteIsLiftable [T <: Byte ] as Liftable [T ] = new PrimitiveLiftable
24
26
given ShortIsLiftable [T <: Short ] as Liftable [T ] = new PrimitiveLiftable
@@ -103,7 +105,7 @@ object Liftable {
103
105
else ' { Array ($ {Expr (array(0 ))}, $ {Expr (array.toSeq.tail)}: _* ) }
104
106
}
105
107
106
- given iArrayIsLiftable [T : Type ](using ltArray : Liftable [Array [T ]]) as Liftable [IArray [T ]] {
108
+ given IArrayIsLiftable [T : Type ](using ltArray : Liftable [Array [T ]]) as Liftable [IArray [T ]] {
107
109
def toExpr (iarray : IArray [T ]): QuoteContext ?=> Expr [IArray [T ]] =
108
110
' { $ {ltArray.toExpr(iarray.asInstanceOf [Array [T ]])}.asInstanceOf [IArray [T ]] }
109
111
}
@@ -118,6 +120,11 @@ object Liftable {
118
120
Expr .ofList(xs.map(summon[Liftable [T ]].toExpr))
119
121
}
120
122
123
+ given NilIsLiftable as Liftable [Nil .type ] = new Liftable [Nil .type ] {
124
+ def toExpr (xs : Nil .type ): QuoteContext ?=> Expr [Nil .type ] =
125
+ ' { Nil }
126
+ }
127
+
121
128
given [T : Type : Liftable ] as Liftable [Set [T ]] = new Liftable [Set [T ]] {
122
129
def toExpr (set : Set [T ]): QuoteContext ?=> Expr [Set [T ]] =
123
130
' { Set ($ {Expr (set.toSeq)}: _* ) }
@@ -130,16 +137,40 @@ object Liftable {
130
137
131
138
given [T : Type : Liftable ] as Liftable [Option [T ]] = new Liftable [Option [T ]] {
132
139
def toExpr (x : Option [T ]): QuoteContext ?=> Expr [Option [T ]] = x match {
133
- case Some (x) => ' { Some [T ]( $ { Expr (x)}) }
134
- case None => ' { None : Option [ T ] }
140
+ case x : Some [T ] => Expr (x)
141
+ case None => Expr ( None )
135
142
}
136
143
}
137
144
145
+ given [T : Type : Liftable ] as Liftable [Some [T ]] = new Liftable [Some [T ]] {
146
+ def toExpr (x : Some [T ]): QuoteContext ?=> Expr [Some [T ]] =
147
+ ' { Some [T ]($ {Expr (x.get)}) }
148
+ }
149
+
150
+ given Liftable [None .type ] = new Liftable [None .type ] {
151
+ def toExpr (x : None .type ): QuoteContext ?=> Expr [None .type ] =
152
+ ' { None }
153
+ }
154
+
138
155
given [L : Type : Liftable , R : Type : Liftable ] as Liftable [Either [L , R ]] = new Liftable [Either [L , R ]] {
139
- def toExpr (x : Either [L , R ]): QuoteContext ?=> Expr [Either [L , R ]] = x match {
140
- case Left (x) => ' { Left [L , R ]($ {Expr (x)}) }
141
- case Right (x) => ' { Right [L , R ]($ {Expr (x)}) }
142
- }
156
+ def toExpr (x : Either [L , R ]): QuoteContext ?=> Expr [Either [L , R ]] = x match
157
+ case x : Left [L , R ] => Expr (x)
158
+ case x : Right [L , R ] => Expr (x)
159
+ }
160
+
161
+ given [L : Type : Liftable , R : Type ] as Liftable [Left [L , R ]] = new Liftable [Left [L , R ]] {
162
+ def toExpr (x : Left [L , R ]): QuoteContext ?=> Expr [Left [L , R ]] =
163
+ ' { Left [L , R ]($ {Expr (x.value)}) }
164
+ }
165
+
166
+ given [L : Type , R : Type : Liftable ] as Liftable [Right [L , R ]] = new Liftable [Right [L , R ]] {
167
+ def toExpr (x : Right [L , R ]): QuoteContext ?=> Expr [Right [L , R ]] =
168
+ ' { Right [L , R ]($ {Expr (x.value)}) }
169
+ }
170
+
171
+ given EmptyTupleIsLiftable as Liftable [EmptyTuple .type ] = new {
172
+ def toExpr (tup : EmptyTuple .type ) =
173
+ ' { EmptyTuple }
143
174
}
144
175
145
176
given [T1 : Type : Liftable ] as Liftable [Tuple1 [T1 ]] = new {
@@ -305,4 +336,11 @@ object Liftable {
305
336
' { BigDecimal ($ {Expr (x.toString)}) }
306
337
}
307
338
339
+ /** Lift a StringContext */
340
+ given Liftable [StringContext ] = new Liftable [StringContext ] {
341
+ def toExpr (stringContext : StringContext ): QuoteContext ?=> Expr [StringContext ] =
342
+ val parts = Varargs (stringContext.parts.map(Expr (_)))
343
+ ' { StringContext ($parts : _* ) }
344
+ }
345
+
308
346
}
0 commit comments