Skip to content

Add quoted.Liftable[BigInt] and quoted.Liftable[BigDecimal] to the stdlib #6944

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 27, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
17 changes: 17 additions & 0 deletions library/src/scala/quoted/Liftable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -232,4 +232,21 @@ object Liftable {
}
}

given as Liftable[BigInt] = new Liftable[BigInt] {
def toExpr(x: BigInt): given QuoteContext => Expr[BigInt] = {
lazy val xInt: Int = x.intValue()
if (x.isValidInt && -1024 <= xInt && xInt < 1024) '{ BigInt(${xInt.toExpr}) } // Cached BigInt
else '{ BigInt(${x.toString(Character.MAX_RADIX).toExpr}, ${Character.MAX_RADIX.toExpr}) } // Compact string BigInt
}
}

/** Lift a BigDecimal using the default MathContext */
given as Liftable[BigDecimal] = new Liftable[BigDecimal] {
def toExpr(x: BigDecimal): given QuoteContext => Expr[BigDecimal] = {
lazy val xInt: Int = x.intValue()
if (x.isValidInt && -512 <= xInt && xInt < 512) '{ BigDecimal(${xInt.toExpr}) } // Cached BigDecimal
else '{ BigDecimal(${x.toString.toExpr}) }
}
}

}
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) }
})
}