Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如何解析 Term 如下所示并提取解析到类应用方法的参数。类 apply 方法接受一个变量参数,因此不知道类构造函数中有多少个参数。
q"""TestClass(TestArg(1,2,"c"), TestArg(10,2,"c"))"""
我需要解析并提取传递给TestClassapply 方法的两个参数(数量将在运行时更改),该方法采用可变数量的参数(vararg)
TestClass
您可以使用点-点取消引用语法来获取Seq[Tree]包含参数的 a,然后分别解压缩它们中的每一个(例如,在map调用中)。
Seq[Tree]
map
val q"TestClass(..$args)" = ??? val result = args.map { case q"""TestArg($a, 2, "c")""" => ??? }