-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* tasty-files-scala-3 * formatting * a feigning hope --------- Co-authored-by: baovitt <[email protected]>
- Loading branch information
Showing
3 changed files
with
46 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
...a-core-modules/scala-core-9/src/main/scala/com/baeldung/scala/tastyfiles/PowerMacro.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.baeldung.scala.tastyfiles | ||
|
||
import scala.quoted.* | ||
|
||
object PowerMacro: | ||
// The macro that unrolls the computation of powers and then generates the expression | ||
inline def showAsPowerTerm(inline x: Double, n: Int): String = ${ | ||
showAsTermImpl('x, 'n) | ||
} | ||
|
||
// The actual implementation of the macro | ||
private def showAsTermImpl(x: Expr[Double], n: Expr[Int])(using | ||
Quotes | ||
): Expr[String] = | ||
import quotes.reflect.* | ||
|
||
n.value match | ||
case Some(num) => | ||
val powerExpr = unrolledPowerCode(x, num) | ||
Expr( | ||
powerExpr.asTerm.toString | ||
) // Ensures that the asTerm method call is evaluated at compile-time | ||
case None => | ||
'{ "Error: 'n' must be a known constant at compile time." } | ||
|
||
// Helper method to unroll the power computation | ||
def unrolledPowerCode(x: Expr[Double], n: Int)(using Quotes): Expr[Double] = | ||
if n == 0 then '{ 1.0 } | ||
else if n == 1 then x | ||
else '{ $x * ${ unrolledPowerCode(x, n - 1) } } |
15 changes: 15 additions & 0 deletions
15
...re-modules/scala-core-9/src/test/scala/com/baeldung/scala/tastyfiles/PowerMacroTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.baeldung.scala.tastyfiles | ||
|
||
import com.baeldung.scala.tastyfiles.* | ||
|
||
import org.scalatest.flatspec.AnyFlatSpec | ||
import org.scalatest.matchers.should.Matchers | ||
|
||
class PowerMacroUnitTest extends AnyFlatSpec with Matchers: | ||
|
||
"PowerMacro.showAsPowerTerm" should "generate compile-time term structure" in: | ||
val expr: String = PowerMacro.showAsPowerTerm(2.0, 3) | ||
|
||
expr == """ | ||
Inlined(Ident(PowerMacro$),List(),Apply(Select(Inlined(EmptyTree,List(),Inlined(EmptyTree,List(),Literal(Constant(2.0)))),*),List(Inlined(EmptyTree,List(),Inlined(Ident(PowerMacro$),List(),Apply(Select(Inlined(EmptyTree,List(),Inlined(EmptyTree,List(),Literal(Constant(2.0)))),*),List(Inlined(EmptyTree,List(),Inlined(EmptyTree,List(),Literal(Constant(2.0))))))))))) | ||
""".trim |