File tree 5 files changed +50
-10
lines changed
docs/_spec/TODOreference/metaprogramming
staging/src/scala/quoted/staging
5 files changed +50
-10
lines changed Original file line number Diff line number Diff line change @@ -108,7 +108,11 @@ to get a source-like representation of the expression.
108
108
import scala .quoted .*
109
109
110
110
// make available the necessary compiler for runtime code generation
111
- given staging .Compiler = staging.Compiler .make(getClass.getClassLoader)
111
+ given staging .Compiler =
112
+ // We need an instance of a class that is defined in the current application (not the standard library)
113
+ // `this` can be used instead of an instance of `Dummy` if the Compiler is instantiated within one of the application classes.
114
+ object Dummy
115
+ staging.Compiler .make(Dummy .getClass.getClassLoader)
112
116
113
117
val f : Array [Int ] => Int = staging.run {
114
118
val stagedSum : Expr [Array [Int ] => Int ] =
Original file line number Diff line number Diff line change @@ -13,15 +13,27 @@ object Compiler:
13
13
14
14
/** Create a new instance of the compiler using the the classloader of the application.
15
15
*
16
- * Usage:
17
- * ```
18
- * import scala.quoted.staging._
19
- * given Compiler = Compiler.make(getClass.getClassLoader)
20
- * ```
16
+ * Usage:
17
+ * ```
18
+ * import scala.quoted.staging._
19
+ * given Compiler =
20
+ * object Dummy
21
+ * Compiler.make(Dummy.getClass.getClassLoader)
22
+ * ```
21
23
*
22
- * @param appClassloader classloader of the application that generated the quotes
23
- * @param settings compiler settings
24
- * @return A new instance of the compiler
24
+ * Note that we use an instance of `Dummy` to get the classloader that loaded the application.
25
+ * Any other instance of a class defined in the application would also work.
26
+ * Using a class defined in the standard library should be avoided as it might be loaded by a different classloader.
27
+ *
28
+ * If the given compiler is defined in one of your classes (e.i. not as a top-level definition), then
29
+ * the compiler can be instantiated with:
30
+ * ```
31
+ * given Compiler = Compiler.make(this.getClass.getClassLoader)
32
+ * ```
33
+ *
34
+ * @param appClassloader classloader of the application that generated the quotes
35
+ * @param settings compiler settings
36
+ * @return A new instance of the compiler
25
37
*/
26
38
def make (appClassloader : ClassLoader )(implicit settings : Settings ): Compiler =
27
39
new Compiler :
Original file line number Diff line number Diff line change @@ -2,7 +2,7 @@ import scala.quoted.*
2
2
3
3
object Test {
4
4
5
- given staging .Compiler = staging.Compiler .make(getClass.getClassLoader)
5
+ given staging .Compiler = staging.Compiler .make(this . getClass.getClassLoader)
6
6
7
7
def main (args : Array [String ]): Unit =
8
8
staging.run {
Original file line number Diff line number Diff line change
1
+ import scala .quoted .*
2
+
3
+ given staging .Compiler =
4
+ object Dummy
5
+ staging.Compiler .make(Dummy .getClass.getClassLoader)
6
+
7
+ class A (i : Int )
8
+
9
+ def f (i : Expr [Int ])(using Quotes ): Expr [A ] = { ' { new A ($i) } }
10
+
11
+ @ main def Test = {
12
+ val g : Int => A = staging.run { ' { (i : Int ) => $ { f(' {i}) } } }
13
+ println(g(3 ))
14
+ }
Original file line number Diff line number Diff line change
1
+ import scala .quoted .*
2
+
3
+ given staging .Compiler =
4
+ object Dummy
5
+ staging.Compiler .make(Dummy .getClass.getClassLoader)
6
+
7
+ class A
8
+ val f : (A , Int ) => Int = staging.run { ' { (q : A , x : Int ) => x } }
9
+
10
+ @ main def Test = f(new A , 3 )
You can’t perform that action at this time.
0 commit comments