取消引用scala宏中应用的类型
在scala控制台中,我可以毫无问题地执行以下操作:取消引用scala宏中应用的类型,scala,macros,types,scala-quasiquotes,Scala,Macros,Types,Scala Quasiquotes,在scala控制台中,我可以毫无问题地执行以下操作: scala> val tree = q"def f():MySuperType[(Char,Char)]" tree: universe.DefDef = def f(): MySuperType[scala.Tuple2[Char, Char]] scala> val q"def $f():$d" = tree f: universe.TermName = f d: universe.Tree = MySuperType[sc
scala> val tree = q"def f():MySuperType[(Char,Char)]"
tree: universe.DefDef = def f(): MySuperType[scala.Tuple2[Char, Char]]
scala> val q"def $f():$d" = tree
f: universe.TermName = f
d: universe.Tree = MySuperType[scala.Tuple2[Char, Char]]
scala> val tq"$a[$TheTypeThatIWant]" = d
a: universe.Tree = MySuperType
TheTypeThatIWant: universe.Tree = scala.Tuple2[Char, Char]
我可以得到我想要的:我想要的类型的内容
现在,如果我尝试在一个Quasikote中这样做,我会得到一个匹配异常,并且我没有找到一种方法来获取应用类型的内部类型
我的代码:
tree match {
case q"{..$body}" =>
body.foreach (_ match {
case q"def $functionName:$type = $osef" =>
val tq"$f[$typ]" = d //I want to get $typ !!
...
}
但我得到的只是:
exception during macro expansion:
exception during macro expansion:
scala.MatchError: MyMacro.MySuperType[(Char, Char)] (of class scala.reflect.internal.Trees$TypeTree)
at MyMacro$$anonfun$getBasicStructure$1$1.apply(MyMacro.scala:737)
at MyMacro$$anonfun$getBasicStructure$1$1.apply(MyMacro.scala:735)
at scala.collection.immutable.List.foreach(List.scala:383)
at MyMacro$.getBasicStructure$1(MyMacro.scala:735)
at MyMacro$.MyMacro_impl(MyMacro.scala:846)
我怎样才能解决这个问题
多谢各位
编辑:
问题不仅在于Quasiqueotes,甚至在我处理树时也会出现bug:
case Block(stats,expr) =>
stats.foreach(_ match {
case DefDef(_,_,_,_,typ,_) =>
typ match {
case AppliedTypeTree(t,args) => //doesnt go there
case TypeApply(t,args) => //doesnt go there
case x:TypeTree => //goes there but can't get any info about the applied type
case _ =>
}
})
Edit2:
你必须这样做:
case q"def $name:${d:TypeTree} = $b" =>
d.tpe match {
case TypeRef(x,y,z) => //z is the list of applied types, see scaladoc
case _ =>
}
嗯,我想这是因为在控制台中,当您调用
val tq“$a[$thetypethatthat]”=d
时,d
的类型实际上是已知的,但在宏中不是这样。这是一个已知的错误:。哦,好的,有什么解决方法吗?修复程序刚刚合并到2.11-RC4。它可能会在下周某个时候被后传到paradise。那么为什么它会在错误中显示类型呢?为什么我可以用show(具体化($d.toString))之类的东西来显示类型?