Skip to content

Commit 725e71a

Browse files
committed
Add forward compat tests for macros
1 parent 300a8b6 commit 725e71a

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import scala.quoted.*
2+
3+
object Power:
4+
5+
inline def power(x: Double, inline n: Int) = ${ powerCode('x, 'n) }
6+
7+
private def powerCode(x: Expr[Double], n: Expr[Int])(using Quotes): Expr[Double] =
8+
unrolledPowerCode(x, n.valueOrError)
9+
10+
private def unrolledPowerCode(x: Expr[Double], n: Int)(using Quotes): Expr[Double] =
11+
if n == 0 then '{ 1.0 }
12+
else if n % 2 == 1 then '{ $x * ${ unrolledPowerCode(x, n - 1) } }
13+
else '{ val y = $x * $x; ${ unrolledPowerCode('y, n / 2) } }
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def testPower(x: Double): Unit =
2+
import Power.power
3+
power(x, 0)
4+
power(x, 1)
5+
power(x, 5)
6+
power(x, 10)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import scala.quoted.*
2+
3+
object Power:
4+
5+
inline def power(x: Double, inline n: Int) = ${ powerCode('x, 'n) }
6+
7+
private def powerCode(x: Expr[Double], n: Expr[Int])(using Quotes): Expr[Double] =
8+
unrolledPowerCode(x, n.valueOrError)
9+
10+
private def unrolledPowerCode(x: Expr[Double], n: Int)(using Quotes): Expr[Double] =
11+
if n == 0 then '{ 1.0 }
12+
else if n % 2 == 1 then '{ $x * ${ unrolledPowerCode(x, n - 1) } }
13+
else '{ val y = $x * $x; ${ unrolledPowerCode('y, n / 2) } }
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def testPower(x: Double): Unit =
2+
import Power.power
3+
power(x, 0)
4+
power(x, 1)
5+
power(x, 5)
6+
power(x, 10)

0 commit comments

Comments
 (0)