@@ -19,6 +19,8 @@ trait Liftable[T] {
1919 */
2020object Liftable {
2121
22+ // IMPORTANT Keep in sync with tests/run-staging/liftables.scala
23+
2224 given BooleanIsLiftable [T <: Boolean ] as Liftable [T ] = new PrimitiveLiftable
2325 given ByteIsLiftable [T <: Byte ] as Liftable [T ] = new PrimitiveLiftable
2426 given ShortIsLiftable [T <: Short ] as Liftable [T ] = new PrimitiveLiftable
@@ -103,7 +105,7 @@ object Liftable {
103105 else ' { Array ($ {Expr (array(0 ))}, $ {Expr (array.toSeq.tail)}: _* ) }
104106 }
105107
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 ]] {
107109 def toExpr (iarray : IArray [T ]): QuoteContext ?=> Expr [IArray [T ]] =
108110 ' { $ {ltArray.toExpr(iarray.asInstanceOf [Array [T ]])}.asInstanceOf [IArray [T ]] }
109111 }
@@ -118,6 +120,11 @@ object Liftable {
118120 Expr .ofList(xs.map(summon[Liftable [T ]].toExpr))
119121 }
120122
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+
121128 given [T : Type : Liftable ] as Liftable [Set [T ]] = new Liftable [Set [T ]] {
122129 def toExpr (set : Set [T ]): QuoteContext ?=> Expr [Set [T ]] =
123130 ' { Set ($ {Expr (set.toSeq)}: _* ) }
@@ -130,16 +137,40 @@ object Liftable {
130137
131138 given [T : Type : Liftable ] as Liftable [Option [T ]] = new Liftable [Option [T ]] {
132139 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 )
135142 }
136143 }
137144
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+
138155 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 }
143174 }
144175
145176 given [T1 : Type : Liftable ] as Liftable [Tuple1 [T1 ]] = new {
@@ -305,4 +336,11 @@ object Liftable {
305336 ' { BigDecimal ($ {Expr (x.toString)}) }
306337 }
307338
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+
308346}
0 commit comments