-
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
f91e5d8
commit 7cd28ef
Showing
3 changed files
with
35 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
hi |
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,24 @@ | ||
import scala.annotation.{experimental, MacroAnnotation} | ||
import scala.quoted._ | ||
|
||
@experimental | ||
class gen extends MacroAnnotation: | ||
def transform(using Quotes)(tree: quotes.reflect.Definition): List[quotes.reflect.Definition] = | ||
import quotes.reflect._ | ||
tree match | ||
case ClassDef(name, ctr, parents, self, body) => | ||
val cls = tree.symbol | ||
// val meth = cls.methodMember("foo").head | ||
// val fooTpe = cls.typeRef.memberType(meth) | ||
|
||
val overrideTpe = MethodType(Nil)(_ => Nil, _ => defn.StringClass.typeRef) | ||
|
||
val fooOverrideSym = Symbol.newMethod(cls, "foo", overrideTpe, Flags.Override, Symbol.noSymbol) | ||
|
||
val fooDef = DefDef(fooOverrideSym, _ => Some(Literal(StringConstant("hi")))) | ||
|
||
val newClassDef = ClassDef.copy(tree)(name, ctr, parents, self, fooDef :: body) | ||
List(newClassDef) | ||
case _ => | ||
report.error("Annotation only supports `class`") | ||
List(tree) |
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,10 @@ | ||
class Base: | ||
def foo(): Object = ??? | ||
|
||
@gen | ||
class Sub extends Base | ||
// > override def foo(): String = "hi" | ||
|
||
@main def Test(): Unit = | ||
val sub = new Sub | ||
println(sub.foo()) |