lundi 20 avril 2015

Handling by-name parameters in Scala macro

I have a macro that does some analysis on nested function applications. It matches applications and retrieve the parameter types this way:

case q"$f[..$targs](..$args)(...$otherArgs)" =>

    // retrieve the list of all parameter types
    val paramTpes = f.tpe match {
      case pmt: PolyType if pmt.paramLists.size == 0 =>
      case pmt: PolyType =>
        pmt.paramLists(0) map {_.typeSignature
             .substituteTypes(pmt.typeParams, targs map (_.tpe))}
      case pmt: MethodType if pmt.paramLists.size == 0 =>
      case pmt: MethodType =>
        pmt.paramLists(0) map (_.typeSignature)

Now, if there happen to be by-name parameters, what I get is some weird type that prints => T but cannot be matched with anything. There seem to be no facility in the reflection API to handle these properly. What I would like to do is retrieve T in case it' a => T, because => T causes further problems in the generated code.

Aucun commentaire:

Enregistrer un commentaire