Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion library/src/scala/quoted/Liftable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,14 @@ object Liftable {
}
}

given [T: Type: Liftable: ClassTag] as Liftable[IArray[T]] = new Liftable[IArray[T]] {
given ArrayIsLiftable[T: Type: Liftable: ClassTag] as Liftable[Array[T]] = new Liftable[Array[T]] {
def toExpr(arr: Array[T]): given QuoteContext => Expr[Array[T]] = '{
val array = new Array[T](${arr.length.toExpr})(ClassTag(${the[ClassTag[T]].runtimeClass.toExpr}))
${ Expr.block(List.tabulate(arr.length)(i => '{ array(${i.toExpr}) = ${arr(i).toExpr} }), '{ array }) }
}
}

given IArrayIsLiftable[T: Type: Liftable: ClassTag] as Liftable[IArray[T]] = new Liftable[IArray[T]] {
def toExpr(iarray: IArray[T]): given QuoteContext => Expr[IArray[T]] = '{
val array = new Array[T](${iarray.length.toExpr})(ClassTag(${the[ClassTag[T]].runtimeClass.toExpr}))
${ Expr.block(List.tabulate(iarray.length)(i => '{ array(${i.toExpr}) = ${iarray(i).toExpr} }), '{ array.asInstanceOf[IArray[T]] }) }
Expand Down Expand Up @@ -232,4 +239,15 @@ object Liftable {
}
}

given as Liftable[BigInt] = new Liftable[BigInt] {
def toExpr(x: BigInt): given QuoteContext => Expr[BigInt] =
'{ BigInt(${x.toByteArray.toExpr}) }
}

/** Lift a BigDecimal using the default MathContext */
given as Liftable[BigDecimal] = new Liftable[BigDecimal] {
def toExpr(x: BigDecimal): given QuoteContext => Expr[BigDecimal] =
'{ BigDecimal(${x.toString.toExpr}) }
}

}
3 changes: 3 additions & 0 deletions tests/run-with-compiler/quote-lib.scala
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ object Test {
val iarray: IArray[Int] = IArray(1, 2, 3)
val liftedIArray: Expr[IArray[Int]] = iarray

val array: Array[Int] = Array(1, 2, 3)
val liftedArray: Expr[Array[Int]] = array

val some: Option[Int] = Some(2)
val none: Option[Int] = Some(2)
val liftedSome: Expr[Option[Int]] = some
Expand Down
1 change: 1 addition & 0 deletions tests/run-with-compiler/quote-lift-BigDecimal.check
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(2.3,1005849025843905834908593485984390583429058574925.95489543)
9 changes: 9 additions & 0 deletions tests/run-with-compiler/quote-lift-BigDecimal.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import scala.quoted._
object Test {
implicit val toolbox: scala.quoted.Toolbox = scala.quoted.Toolbox.make(getClass.getClassLoader)
def main(args: Array[String]): Unit = println(run {
val a = BigDecimal(2.3).toExpr
val b = BigDecimal("1005849025843905834908593485984390583429058574925.95489543").toExpr
'{ ($a, $b) }
})
}
1 change: 1 addition & 0 deletions tests/run-with-compiler/quote-lift-BigInt.check
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(2,1005849025843905834908593485984390583429058574925)
9 changes: 9 additions & 0 deletions tests/run-with-compiler/quote-lift-BigInt.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import scala.quoted._
object Test {
implicit val toolbox: scala.quoted.Toolbox = scala.quoted.Toolbox.make(getClass.getClassLoader)
def main(args: Array[String]): Unit = println(run {
val a = BigInt(2).toExpr
val b = BigInt("1005849025843905834908593485984390583429058574925").toExpr
'{ ($a, $b) }
})
}