Skip to content

Commit 0b1fe9d

Browse files
committed
Add quoted.Liftable[BigInt] to the stdlib
1 parent 3311e22 commit 0b1fe9d

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

library/src/scala/quoted/Liftable.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,4 +240,13 @@ object Liftable {
240240
}
241241
}
242242

243+
/** Lift a BigDecimal using the default MathContext */
244+
given as Liftable[BigDecimal] = new Liftable[BigDecimal] {
245+
def toExpr(x: BigDecimal): given QuoteContext => Expr[BigDecimal] = {
246+
lazy val xInt: Int = x.intValue()
247+
if (x.isValidInt && -512 <= xInt && xInt < 512) '{ BigDecimal(${xInt.toExpr}) } // Cached BigDecimal
248+
else '{ BigDecimal(${x.toString.toExpr}) }
249+
}
250+
}
251+
243252
}
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+
}

0 commit comments

Comments
 (0)