-
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
8922c88
commit 7def96e
Showing
13 changed files
with
208 additions
and
2 deletions.
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
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) | ||
} |
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 @@ | ||
Calling Bar.bar | ||
Calling Foo.foo | ||
Calling Bar.bar | ||
class Test_2$package$A$1 | ||
Calling Bar.bar | ||
Calling Foo.foo | ||
Calling Bar.bar | ||
class Test_2$package$B$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,71 @@ | ||
import scala.quoted.* | ||
|
||
inline def makeClass(inline name: String): Bar = ${ makeClassExpr('name) } | ||
private def makeClassExpr(nameExpr: Expr[String])(using Quotes): Expr[Bar] = { | ||
import quotes.reflect.* | ||
val name = nameExpr.valueOrAbort | ||
val fooDef = makeFoo() | ||
val fooBarDef = makeFooBar(name, fooDef.symbol) | ||
val newCls = makeNewFooBar(fooBarDef.symbol) | ||
|
||
Block(List(fooDef, fooBarDef), newCls).asExprOf[Bar] | ||
// '{ | ||
// class Foo { self: Bar => | ||
// def foo(): Unit = bar() | ||
// } | ||
// class `name`() extends Foo with Bar | ||
// new `name`() | ||
// } | ||
} | ||
|
||
/** Generate | ||
* ``` | ||
* class Foo { self: Bar => | ||
* def foo(): Unit = bar() | ||
* } | ||
* ``` | ||
*/ | ||
def makeFoo(using Quotes)(): quotes.reflect.ClassDef = { | ||
import quotes.reflect.* | ||
val parents = List(TypeTree.of[Object]) | ||
def decls(cls: Symbol): List[Symbol] = | ||
List(Symbol.newMethod(cls, "foo", MethodType(Nil)(_ => Nil, _ => TypeRepr.of[Unit]))) | ||
|
||
val cls = Symbol.newClass(Symbol.spliceOwner, "Foo", parents = parents.map(_.tpe), decls, selfInfo = Some(TypeRepr.of[Bar])) | ||
val fooSym = cls.declaredMethod("foo").head | ||
val barSym = Symbol.classSymbol("Bar").declaredMethod("bar").head | ||
|
||
def fooRhs(args: List[List[Tree]]): Option[Term] = | ||
val barCall = This(cls).select(barSym).appliedToNone.asExprOf[Unit] | ||
Some('{ println("Calling Foo.foo"); $barCall }.asTerm) | ||
|
||
val fooDef = DefDef(fooSym, fooRhs) | ||
ClassDef(cls, parents, body = List(fooDef)) | ||
} | ||
|
||
/** Generate | ||
* ``` | ||
* class `name`() extends Foo with Bar | ||
* ``` | ||
*/ | ||
def makeFooBar(using Quotes)(name: String, fooCls: quotes.reflect.Symbol): quotes.reflect.ClassDef = { | ||
import quotes.reflect.* | ||
val parents = List(Inferred(fooCls.typeRef), TypeTree.of[Bar]) | ||
def decls(cls: Symbol): List[Symbol] = Nil | ||
val cls = Symbol.newClass(Symbol.spliceOwner, name, parents = parents.map(_.tpe), decls, selfInfo = None) | ||
ClassDef(cls, parents, body = Nil) | ||
} | ||
|
||
/** Generate | ||
* ``` | ||
* new `name`() | ||
* ``` | ||
*/ | ||
def makeNewFooBar(using Quotes)(fooBarCls: quotes.reflect.Symbol): quotes.reflect.Term = { | ||
import quotes.reflect.* | ||
Typed(Apply(Select(New(TypeIdent(fooBarCls)), fooBarCls.primaryConstructor), Nil), TypeTree.of[Bar]) | ||
} | ||
|
||
trait Bar { | ||
def bar(): Unit = println("Calling Bar.bar") | ||
} |
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,13 @@ | ||
@main def Test: Unit = { | ||
val a: Bar = makeClass("A") | ||
a.bar() | ||
callFoo(a) | ||
println(a.getClass) | ||
val b: Bar = makeClass("B") | ||
b.bar() | ||
callFoo(b) | ||
println(b.getClass) | ||
} | ||
|
||
def callFoo(x: Any): Unit = | ||
x.getClass.getMethod("foo").invoke(x) |