-
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.
Quotes reflect: Allow to return DefDef from a val symbol tree
- Loading branch information
Showing
5 changed files
with
52 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
//> using options -Yretain-trees | ||
|
||
import scala.quoted.* | ||
|
||
object Macros { | ||
|
||
inline def myMacro[A]: Unit = ${ myMacroImpl[A] } | ||
|
||
private def myMacroImpl[A](using quotes: Quotes, tpe: Type[A]): Expr[Unit] = { | ||
import quotes.reflect.* | ||
|
||
val typeRepr = TypeRepr.of[A] | ||
val typeSymbol = typeRepr.typeSymbol | ||
|
||
val caseFieldSymbols: List[Symbol] = typeSymbol.fieldMembers | ||
val caseFieldValOrDefDefs: List[ValDef | DefDef] = | ||
caseFieldSymbols.sortBy(_.toString).map { | ||
_.tree match { | ||
case valDef: ValDef => valDef | ||
case defDef: DefDef => defDef | ||
case _ => report.errorAndAbort("???") | ||
} | ||
} | ||
|
||
val expected = "List(DefDef(boolean,List(List()),TypeTree[TypeRef(ThisType(TypeRef(NoPrefix,module class scala)),class Boolean)],Select(This(Ident(MyClass1)),boolean)), DefDef(finalVal,List(List()),TypeTree[TypeRef(ThisType(TypeRef(NoPrefix,module class lang)),class String)],Select(This(Ident(MyClass1)),finalVal)), DefDef(int,List(List()),TypeTree[TypeRef(ThisType(TypeRef(NoPrefix,module class scala)),class Int)],Select(This(Ident(MyClass1)),int)), DefDef(string,List(List()),TypeTree[TypeRef(ThisType(TypeRef(NoPrefix,module class lang)),class String)],Select(This(Ident(MyClass1)),string)))" | ||
assert(caseFieldValOrDefDefs.toString == expected) | ||
|
||
'{ () } | ||
} | ||
} |
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 @@ | ||
import Types.* | ||
|
||
@main def main() = | ||
Macros.myMacro[MyClass1] |
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,9 @@ | ||
object Types { | ||
final case class MyClass1( | ||
int: Int, | ||
string: String, | ||
boolean: Boolean, | ||
) { | ||
final val finalVal: String = "result" | ||
} | ||
} |