Skip to content

Commit aab620a

Browse files
bradovittbaovitt
andauthored
tasty-files-scala-3 (#1457)
* tasty-files-scala-3 * formatting * a feigning hope --------- Co-authored-by: baovitt <[email protected]>
1 parent cfa6606 commit aab620a

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

scala-core-modules/scala-core-9/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
- [Find Two’s Complement of a Number in Scala](https://www.baeldung.com/scala/compute-twos-complement)
77
- [Convert Option to Either in Scala](https://www.baeldung.com/scala/option-either-conversion)
88
- [Meaning of _root_ In Scala Import Clause](https://www.baeldung.com/scala/root-import-clause)
9-
- [Check if All Characters in a Scala String Are Either Upper or Lower Case](https://www.baeldung.com/scala/string-check-all-characters-upper-lower-case)
9+
- [Check if All Characters in a Scala String Are Either Upper or Lower Case](https://www.baeldung.com/scala/string-check-all-characters-upper-lower-case
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.baeldung.scala.tastyfiles
2+
3+
import scala.quoted.*
4+
5+
object PowerMacro:
6+
// The macro that unrolls the computation of powers and then generates the expression
7+
inline def showAsPowerTerm(inline x: Double, n: Int): String = ${
8+
showAsTermImpl('x, 'n)
9+
}
10+
11+
// The actual implementation of the macro
12+
private def showAsTermImpl(x: Expr[Double], n: Expr[Int])(using
13+
Quotes
14+
): Expr[String] =
15+
import quotes.reflect.*
16+
17+
n.value match
18+
case Some(num) =>
19+
val powerExpr = unrolledPowerCode(x, num)
20+
Expr(
21+
powerExpr.asTerm.toString
22+
) // Ensures that the asTerm method call is evaluated at compile-time
23+
case None =>
24+
'{ "Error: 'n' must be a known constant at compile time." }
25+
26+
// Helper method to unroll the power computation
27+
def unrolledPowerCode(x: Expr[Double], n: Int)(using Quotes): Expr[Double] =
28+
if n == 0 then '{ 1.0 }
29+
else if n == 1 then x
30+
else '{ $x * ${ unrolledPowerCode(x, n - 1) } }
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.scala.tastyfiles
2+
3+
import com.baeldung.scala.tastyfiles.*
4+
5+
import org.scalatest.flatspec.AnyFlatSpec
6+
import org.scalatest.matchers.should.Matchers
7+
8+
class PowerMacroUnitTest extends AnyFlatSpec with Matchers:
9+
10+
"PowerMacro.showAsPowerTerm" should "generate compile-time term structure" in:
11+
val expr: String = PowerMacro.showAsPowerTerm(2.0, 3)
12+
13+
expr == """
14+
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)))))))))))
15+
""".trim

0 commit comments

Comments
 (0)