use*_*260 2 scala scala-macros scala-3
这是基本设置:
trait MyProduct[A, B](a: A, b: B)
class X
class Y
class XxY(x: X, y: Y) extends MyProduct(x,y)
Run Code Online (Sandbox Code Playgroud)
我正在尝试检查该MyProduct特征的论据。更具体地说,我想提取一些信息,例如字符串“x”和“y”,它们指示 的哪些字段XxY传递给MyProduct.
重要的是,我需要满足具有相同类型的多个字段的情况,例如class XxYxY(x: X, y1: Y, y2: Y) extends MyProduct3(x, y1, y2),因此从类型参数进行推理是不够的。
我想也许问题是我还没有找到一种方法来为条款extends本身添加符号。我可以找到ClassDeffor XxY,从中我可以提取parents并获取已经构造的类型MyProduct[X,Y]。我还查看了symbol.declarations封闭模块和XxY.<init>函数的 ,以查看是否有可用于查找参数的数据,但这些似乎都没有我正在寻找的信息。
查看封闭模块的树让我认为这些信息可能被删除,而是需要从源代码解析为文本,但我希望有人有更好的解决方案。
从评论中编辑:
作为输入,我有一个Type封闭对象/模块的实例。例如:
trait MyProduct[A, B](a: A, b: B)
class X
class Y
class XxY(x: X, y: Y) extends MyProduct(x,y)
Run Code Online (Sandbox Code Playgroud)
作为输出,我想要一些允许我检查 extends 子句中使用的特征的参数的东西。
如果你这样做
import scala.quoted.*
object Macros {
inline def simpleProductTest[T]: Unit = ${simpleProductTestImpl[T]}
def simpleProductTestImpl[T](using Quotes)(using Type[T]): Expr[Unit] = {
import quotes.reflect.*
println(TypeRepr.of[T].typeSymbol.tree.asInstanceOf[TypeDef].rhs)
'{()}
}
}
Run Code Online (Sandbox Code Playgroud)
你会看到的
object App {
trait MyProduct[A, B](a: A, b: B)
class X
class Y
class XxY(x: X, y: Y, z: Int) extends MyProduct(x,y)
Macros.simpleProductTest[XxY]
}
Run Code Online (Sandbox Code Playgroud)
没有关于的信息x,y
Template(
DefDef(
<init>,
List(List(
ValDef(x,TypeTree[TypeRef(ThisType(TypeRef(ThisType(TypeRef(NoPrefix,module class <empty>)),module class App$)),class X)],EmptyTree),
ValDef(y,TypeTree[TypeRef(ThisType(TypeRef(ThisType(TypeRef(NoPrefix,module class <empty>)),module class App$)),class Y)],EmptyTree),
ValDef(z,TypeTree[TypeRef(TermRef(ThisType(TypeRef(NoPrefix,module class <root>)),object scala),class Int)],EmptyTree)
)),
TypeTree[TypeRef(ThisType(TypeRef(ThisType(TypeRef(NoPrefix,module class <empty>)),module class App$)),class XxY)],
EmptyTree
),
List(
TypeTree[TypeRef(ThisType(TypeRef(NoPrefix,module class lang)),class Object)],
TypeTree[AppliedType(TypeRef(ThisType(TypeRef(ThisType(TypeRef(NoPrefix,module class <empty>)),module class App$)),trait MyProduct),
List(
TypeRef(ThisType(TypeRef(ThisType(TypeRef(NoPrefix,module class <empty>)),module class App$)),class X),
TypeRef(ThisType(TypeRef(ThisType(TypeRef(NoPrefix,module class <empty>)),module class App$)),class Y)
)
)]
),
ValDef(_,EmptyTree,EmptyTree),
List(
ValDef(x,TypeTree[TypeRef(ThisType(TypeRef(ThisType(TypeRef(NoPrefix,module class <empty>)),module class App$)),class X)],EmptyTree),
ValDef(y,TypeTree[TypeRef(ThisType(TypeRef(ThisType(TypeRef(NoPrefix,module class <empty>)),module class App$)),class Y)],EmptyTree),
ValDef(z,TypeTree[TypeRef(TermRef(ThisType(TypeRef(NoPrefix,module class <root>)),object scala),class Int)],EmptyTree)
)
)
Run Code Online (Sandbox Code Playgroud)
并且Template不存在于scala.quoted.*,它是dotty.tools.dotc.ast.Trees.Template。
但如果你用TASTy 检查.tasty打印文件
libraryDependencies += "org.scala-lang" %% "scala3-tasty-inspector" % "3.0.0"
import scala.quoted.*
import scala.tasty.inspector.*
class MyInspector extends Inspector:
def inspect(using Quotes)(tastys: List[Tasty[quotes.type]]): Unit =
import quotes.reflect.*
for tasty <- tastys do
val tree = tasty.ast
println(tree)
@main def test: Unit =
val tastyFiles = List("target/scala-3.0.0/classes/App.tasty")
TastyInspector.inspectTastyFiles(tastyFiles)(new MyInspector)
Run Code Online (Sandbox Code Playgroud)
你会看到的
Template(
DefDef(
<init>,
List(List(
ValDef(x,Ident(X),EmptyTree),
ValDef(y,Ident(Y),EmptyTree),
ValDef(z,Ident(Int),EmptyTree)
)),
TypeTree[TypeRef(TermRef(ThisType(TypeRef(NoPrefix,module class <root>)),object scala),Unit)],
EmptyTree
),
List(
Apply(Select(New(TypeTree[TypeRef(TermRef(ThisType(TypeRef(NoPrefix,module class java)),object lang),Object)]),<init>),List()),
Apply(
TypeApply(
Select(New(Ident(MyProduct)),<init>),
List(
TypeTree[TypeRef(ThisType(TypeRef(ThisType(TypeRef(ThisType(TypeRef(NoPrefix,module class <root>)),module class <empty>)),module class App$)),class X)],
TypeTree[TypeRef(ThisType(TypeRef(ThisType(TypeRef(ThisType(TypeRef(NoPrefix,module class <root>)),module class <empty>)),module class App$)),class Y)]
)
),
List(Ident(x), Ident(y)) <--- HERE!!!
)
),
ValDef(_,EmptyTree,EmptyTree),
List(
ValDef(x,TypeTree[TypeRef(ThisType(TypeRef(ThisType(TypeRef(ThisType(TypeRef(NoPrefix,module class <root>)),module class <empty>)),module class App$)),class X)],EmptyTree),
ValDef(y,TypeTree[TypeRef(ThisType(TypeRef(ThisType(TypeRef(ThisType(TypeRef(NoPrefix,module class <root>)),module class <empty>)),module class App$)),class Y)],EmptyTree),
ValDef(z,TypeTree[TypeRef(TermRef(ThisType(TypeRef(NoPrefix,module class <root>)),object scala),Int)],EmptyTree)
)
)
Run Code Online (Sandbox Code Playgroud)
这棵树略有不同,并且x,y有相关信息。
在 Scaladocs 中编写了它的.tree扩展方法Symbol
* **Warning**: avoid using this method in macros.
*
* **Caveat**: The tree is not guaranteed to exist unless the compiler
* option `-Yretain-trees` is enabled.
Run Code Online (Sandbox Code Playgroud)
让我们开启吧-Yretain-trees
scalacOptions += "-Yretain-trees"
然后Macros.simpleProductTest[XxY]打印
Template(
DefDef(
<init>,
List(List(
ValDef(x,Ident(X),EmptyTree),
ValDef(y,Ident(Y),EmptyTree),
ValDef(z,Ident(Int),EmptyTree)
)),
TypeTree[TypeRef(ThisType(TypeRef(NoPrefix,module class scala)),class Unit)],
EmptyTree
),
List(
Apply(Select(New(TypeTree[TypeRef(ThisType(TypeRef(NoPrefix,module class lang)),class Object)]),<init>),List()),
Apply(
TypeApply(
Select(New(Ident(MyProduct)),<init>),
List(
TypeTree[TypeVar(TypeParamRef(A) -> TypeRef(ThisType(TypeRef(ThisType(TypeRef(NoPrefix,module class <empty>)),module class App$)),class X))],
TypeTree[TypeVar(TypeParamRef(B) -> TypeRef(ThisType(TypeRef(ThisType(TypeRef(NoPrefix,module class <empty>)),module class App$)),class Y))]
)
),
List(Ident(x), Ident(y)) <---- AGAIN HERE !!!!
)
),
ValDef(_,EmptyTree,EmptyTree),
List(
ValDef(x,TypeTree[TypeRef(ThisType(TypeRef(ThisType(TypeRef(NoPrefix,module class <empty>)),module class App$)),class X)],EmptyTree),
ValDef(y,TypeTree[TypeRef(ThisType(TypeRef(ThisType(TypeRef(NoPrefix,module class <empty>)),module class App$)),class Y)],EmptyTree),
ValDef(z,TypeTree[TypeRef(TermRef(ThisType(TypeRef(NoPrefix,module class <root>)),object scala),class Int)],EmptyTree)
)
)
Run Code Online (Sandbox Code Playgroud)
即树再次略有不同,但x,y出现了相关信息。所以现在的问题只是如何提取这些信息,因为它是dotty.tools.dotc.ast.*
case class Template[-T >: Untyped] private[ast](
constr: DefDef[T],
parentsOrDerived: List[Tree[T]],
self: ValDef[T],
private var preBody: LazyTreeList[T @uncheckedVariance]
)(implicit @constructorOnly src: SourceFile)
Run Code Online (Sandbox Code Playgroud)
所以最后
libraryDependencies += "org.scala-lang" %% "scala3-compiler" % "3.0.0"
import scala.quoted.*
inline def simpleProductTest[T]: List[String] = ${simpleProductTestImpl[T]}
def simpleProductTestImpl[T](using Quotes)(using Type[T]): Expr[List[String]] = {
import quotes.reflect.*
Expr(
TypeRepr.of[T].typeSymbol.tree.asInstanceOf[TypeDef].rhs
.asInstanceOf[dotty.tools.dotc.ast.Trees.Template[?]]
.parentsOrDerived
.asInstanceOf[List[Apply]]
.tail.head.args.map(_.symbol.name)
)
}
Macros.simpleProductTest[XxY] // List(x, y)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
346 次 |
| 最近记录: |