Skip to content

Commit 3311e22

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

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

library/src/scala/quoted/Liftable.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,4 +232,12 @@ object Liftable {
232232
}
233233
}
234234

235+
given as Liftable[BigInt] = new Liftable[BigInt] {
236+
def toExpr(x: BigInt): given QuoteContext => Expr[BigInt] = {
237+
lazy val xInt: Int = x.intValue()
238+
if (x.isValidInt && -1024 <= xInt && xInt < 1024) '{ BigInt(${xInt.toExpr}) } // Cached BigInt
239+
else '{ BigInt(${x.toString(Character.MAX_RADIX).toExpr}, ${Character.MAX_RADIX.toExpr}) } // Compact string BigInt
240+
}
241+
}
242+
235243
}
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)