From 95741a2180e22f770611b6c77c7c08bb443c0856 Mon Sep 17 00:00:00 2001 From: Oleg Zenzin Date: Tue, 17 Dec 2024 19:02:49 -0800 Subject: [PATCH 1/3] Update multi-stage example to reference latest example from Macros section Current Example is based on obsolete content of macros-spec.md (which was updated in commit d8c9714) . This small change brings in the updated content here as well. --- docs/_docs/reference/metaprogramming/staging.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/docs/_docs/reference/metaprogramming/staging.md b/docs/_docs/reference/metaprogramming/staging.md index 1c154e09f50e..814bf0e4badb 100644 --- a/docs/_docs/reference/metaprogramming/staging.md +++ b/docs/_docs/reference/metaprogramming/staging.md @@ -110,12 +110,12 @@ import scala.quoted.* // make available the necessary compiler for runtime code generation given staging.Compiler = staging.Compiler.make(getClass.getClassLoader) -val f: Array[Int] => Int = staging.run { - val stagedSum: Expr[Array[Int] => Int] = - '{ (arr: Array[Int]) => ${sum('arr)}} - println(stagedSum.show) // Prints "(arr: Array[Int]) => { var sum = 0; ... }" - stagedSum +def power3: Double => Double = staging.run { + val stagedPower3: Expr[Double => Double] = + '{ (x: Double) => ${ unrolledPowerCode('x, 3) } } + println(stagedPower3.show) // Prints "((x: scala.Double) => x.*(x.*(x)))" + stagedPower3 } -f.apply(Array(1, 2, 3)) // Returns 6 +power3.apply(2.0) // Returns 8.0 ``` From 50bb1b8c1c08a6710e0b985ad646d70ae18d72a6 Mon Sep 17 00:00:00 2001 From: Oleg Zenzin Date: Tue, 17 Dec 2024 19:48:44 -0800 Subject: [PATCH 2/3] Update staging.md - tiny beautification --- docs/_docs/reference/metaprogramming/staging.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/_docs/reference/metaprogramming/staging.md b/docs/_docs/reference/metaprogramming/staging.md index 814bf0e4badb..3c5ea4515855 100644 --- a/docs/_docs/reference/metaprogramming/staging.md +++ b/docs/_docs/reference/metaprogramming/staging.md @@ -110,7 +110,7 @@ import scala.quoted.* // make available the necessary compiler for runtime code generation given staging.Compiler = staging.Compiler.make(getClass.getClassLoader) -def power3: Double => Double = staging.run { +val power3: Double => Double = staging.run { val stagedPower3: Expr[Double => Double] = '{ (x: Double) => ${ unrolledPowerCode('x, 3) } } println(stagedPower3.show) // Prints "((x: scala.Double) => x.*(x.*(x)))" From c640e550d8cd2e28ab500364d1d82a647cef1545 Mon Sep 17 00:00:00 2001 From: Oleg Zenzin Date: Thu, 19 Dec 2024 10:48:40 -0800 Subject: [PATCH 3/3] Fixing staging.md Example linked to obsolete macros-spec.md content Missed a wording still referencing obsolete content (thanks code reviewers for pointing it out) --- docs/_docs/reference/metaprogramming/staging.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/_docs/reference/metaprogramming/staging.md b/docs/_docs/reference/metaprogramming/staging.md index 3c5ea4515855..001ae622eabc 100644 --- a/docs/_docs/reference/metaprogramming/staging.md +++ b/docs/_docs/reference/metaprogramming/staging.md @@ -98,9 +98,9 @@ scala -with-compiler -classpath out Test ## Example Now take exactly the same example as in [Macros](./macros.md). Assume that we -do not want to pass an array statically but generate code at run-time and pass +do not want to pass a base double value statically but generate code at run-time and pass the value, also at run-time. Note, how we make a future-stage function of type -`Expr[Array[Int] => Int]` in line 6 below. Using `staging.run { ... }` we can evaluate an +`Expr[Double => Double]` in line 6 below. Using `staging.run { ... }` we can evaluate an expression at runtime. Within the scope of `staging.run` we can also invoke `show` on an expression to get a source-like representation of the expression.