Skip to content

Commit 3181374

Browse files
Merge pull request #6944 from dotty-staging/add-bignumbers-liftable
Add `quoted.Liftable[BigInt]` and `quoted.Liftable[BigDecimal]` to the stdlib
2 parents 1bb0c64 + 2e083d5 commit 3181374

File tree

6 files changed

+42
-1
lines changed

6 files changed

+42
-1
lines changed

library/src/scala/quoted/Liftable.scala

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,14 @@ object Liftable {
4545
}
4646
}
4747

48-
given [T: Type: Liftable: ClassTag] as Liftable[IArray[T]] = new Liftable[IArray[T]] {
48+
given ArrayIsLiftable[T: Type: Liftable: ClassTag] as Liftable[Array[T]] = new Liftable[Array[T]] {
49+
def toExpr(arr: Array[T]): given QuoteContext => Expr[Array[T]] = '{
50+
val array = new Array[T](${arr.length.toExpr})(ClassTag(${the[ClassTag[T]].runtimeClass.toExpr}))
51+
${ Expr.block(List.tabulate(arr.length)(i => '{ array(${i.toExpr}) = ${arr(i).toExpr} }), '{ array }) }
52+
}
53+
}
54+
55+
given IArrayIsLiftable[T: Type: Liftable: ClassTag] as Liftable[IArray[T]] = new Liftable[IArray[T]] {
4956
def toExpr(iarray: IArray[T]): given QuoteContext => Expr[IArray[T]] = '{
5057
val array = new Array[T](${iarray.length.toExpr})(ClassTag(${the[ClassTag[T]].runtimeClass.toExpr}))
5158
${ Expr.block(List.tabulate(iarray.length)(i => '{ array(${i.toExpr}) = ${iarray(i).toExpr} }), '{ array.asInstanceOf[IArray[T]] }) }
@@ -232,4 +239,15 @@ object Liftable {
232239
}
233240
}
234241

242+
given as Liftable[BigInt] = new Liftable[BigInt] {
243+
def toExpr(x: BigInt): given QuoteContext => Expr[BigInt] =
244+
'{ BigInt(${x.toByteArray.toExpr}) }
245+
}
246+
247+
/** Lift a BigDecimal using the default MathContext */
248+
given as Liftable[BigDecimal] = new Liftable[BigDecimal] {
249+
def toExpr(x: BigDecimal): given QuoteContext => Expr[BigDecimal] =
250+
'{ BigDecimal(${x.toString.toExpr}) }
251+
}
252+
235253
}

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

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

71+
val array: Array[Int] = Array(1, 2, 3)
72+
val liftedArray: Expr[Array[Int]] = array
73+
7174
val some: Option[Int] = Some(2)
7275
val none: Option[Int] = Some(2)
7376
val liftedSome: Expr[Option[Int]] = some
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
(2.3,1005849025843905834908593485984390583429058574925.95489543)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import scala.quoted._
2+
object Test {
3+
implicit val toolbox: scala.quoted.Toolbox = scala.quoted.Toolbox.make(getClass.getClassLoader)
4+
def main(args: Array[String]): Unit = println(run {
5+
val a = BigDecimal(2.3).toExpr
6+
val b = BigDecimal("1005849025843905834908593485984390583429058574925.95489543").toExpr
7+
'{ ($a, $b) }
8+
})
9+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
(2,1005849025843905834908593485984390583429058574925)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import scala.quoted._
2+
object Test {
3+
implicit val toolbox: scala.quoted.Toolbox = scala.quoted.Toolbox.make(getClass.getClassLoader)
4+
def main(args: Array[String]): Unit = println(run {
5+
val a = BigInt(2).toExpr
6+
val b = BigInt("1005849025843905834908593485984390583429058574925").toExpr
7+
'{ ($a, $b) }
8+
})
9+
}

0 commit comments

Comments
 (0)