-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4f3d767
commit 647629e
Showing
9 changed files
with
101 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
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
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
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,4 @@ | ||
Constructing foo | ||
class Test_2$package$foo$1 | ||
Constructing bar | ||
class Test_2$package$bar$1 |
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,21 @@ | ||
import scala.quoted.* | ||
|
||
inline def makeClass(inline name: String): Any = ${ makeClassExpr('name) } | ||
private def makeClassExpr(nameExpr: Expr[String])(using Quotes): Expr[Any] = { | ||
import quotes.reflect.* | ||
|
||
val name = nameExpr.valueOrAbort | ||
val parents = List(TypeTree.of[Object]) | ||
def decls(cls: Symbol): List[Symbol] = Nil | ||
|
||
val cls = Symbol.newClass(Symbol.spliceOwner, name, parents = parents.map(_.tpe), decls, selfInfo = None) | ||
|
||
val clsDef = ClassDef(cls, parents, body = List('{println(s"Constructing ${$nameExpr}")}.asTerm)) | ||
val newCls = Typed(Apply(Select(New(TypeIdent(cls)), cls.primaryConstructor), Nil), TypeTree.of[Object]) | ||
|
||
Block(List(clsDef), newCls).asExpr | ||
// '{ | ||
// class `name`() { println("Constructing `name`") } | ||
// new `name`() | ||
// } | ||
} |
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,6 @@ | ||
@main def Test: Unit = { | ||
val foo = makeClass("foo") | ||
println(foo.getClass) | ||
val bar = makeClass("bar") | ||
println(bar.getClass) | ||
} |
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,4 @@ | ||
Calling foo.foo | ||
class Test_2$package$foo$1 | ||
Calling bar.foo | ||
class Test_2$package$bar$1 |
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,31 @@ | ||
import scala.quoted.* | ||
|
||
inline def makeClass(inline name: String): Foo = ${ makeClassExpr('name) } | ||
private def makeClassExpr(nameExpr: Expr[String])(using Quotes): Expr[Foo] = { | ||
import quotes.reflect.* | ||
|
||
val name = nameExpr.valueOrAbort | ||
val parents = List(TypeTree.of[Object], TypeTree.of[Foo]) | ||
def decls(cls: Symbol): List[Symbol] = | ||
List(Symbol.newMethod(cls, "foo", MethodType(Nil)(_ => Nil, _ => TypeRepr.of[Unit]))) | ||
|
||
val cls = Symbol.newClass(Symbol.spliceOwner, name, parents = parents.map(_.tpe), decls, selfInfo = None) | ||
val fooSym = cls.declaredMethod("foo").head | ||
|
||
val fooDef = DefDef(fooSym, argss => Some('{println(s"Calling ${$nameExpr}.foo")}.asTerm)) | ||
val clsDef = ClassDef(cls, parents, body = List(fooDef)) | ||
val newCls = Typed(Apply(Select(New(TypeIdent(cls)), cls.primaryConstructor), Nil), TypeTree.of[Foo]) | ||
|
||
Block(List(clsDef), newCls).asExprOf[Foo] | ||
|
||
// '{ | ||
// class `name`() extends Foo { | ||
// def foo(): Unit = println("Calling `name`.foo") | ||
// } | ||
// new `name`() | ||
// } | ||
} | ||
|
||
trait Foo { | ||
def foo(): Unit | ||
} |
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,8 @@ | ||
@main def Test: Unit = { | ||
val foo: Foo = makeClass("foo") | ||
foo.foo() | ||
println(foo.getClass) | ||
val bar: Foo = makeClass("bar") | ||
bar.foo() | ||
println(bar.getClass) | ||
} |