2

In the scala console I can do the following without a problem :

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]

And I can get what I want : the content of TheTypeThatIWant

Now If I try to do that inside a quasiquote, I get a match exception and I didn't find a way to get the inner type of an applied type.

My code :

tree match {
        case q"{..$body}" =>
          body.foreach (_ match {
            case q"def $functionName:$type = $osef" =>
              val tq"$f[$typ]" = d //I want to get $typ !!
              ...
}

But all I get is :

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)

How can I solve that ?

Thank you

Edit :

The problem is not only with quasiquotes, it bugs even when I work with 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 _ =>
               }
           })

Edit2 :

You have to do it that way :

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 _ =>
              }
tir0nik
  • 31
  • 3

1 Answers1

0

Well, I guess that's because in the console, by the time you call val tq"$a[$TheTypeThatIWant]" = d , the type of d is actually known, but it's not the case in the macro.

Ashalynd
  • 12,363
  • 2
  • 34
  • 37
  • Then why does it display the type in the error ? And why can I display the type with something like show(reify($d.toString)) ? – tir0nik Mar 17 '14 at 10:37