2

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[scala.Tuple2[Char, Char]]

scala> val tq"$a[$TheTypeThatIWant]" = d
a: universe.Tree = MySuperType
TheTypeThatIWant: universe.Tree = scala.Tuple2[Char, Char]

そして、私が欲しいものを手に入れることができます: TheTypeThatIWantのコンテンツ

quasiquote 内でこれを実行しようとすると、一致例外が発生し、適用された型の内部型を取得する方法が見つかりませんでした。

私のコード:

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)

どうすれば解決できますか?

ありがとうございました

編集 :

問題は quasiquotes だけでなく、 Trees で作業してもバグが発生します。

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 _ =>
               }
           })

編集2:

あなたはそのようにしなければなりません:

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 _ =>
              }
4

1 に答える 1