2013-07-15 27 views
7

有沒有方法可以使用宏返回包下的每個類的ListTypeSymbol斯卡拉宏:獲取在運行時使用的TypeSymbols列表

我所試圖實現的是寫一個宏,給出了一些東西相當於這個名單:

scala> import scala.reflect.runtime.universe._ 
import scala.reflect.runtime.universe._ 

scala> case class MyClass1() 
defined class MyClass1 

scala> case class MyClass2() 
defined class MyClass2 

scala> val typeSymbols = List(typeOf[MyClass1].typeSymbol, typeOf[MyClass2].typeSymbol) 
typeSymbols: List[reflect.runtime.universe.Symbol] = List(class MyClass1, class MyClass2) 

這裏是我的設置:

我有一個名爲foo包,其下這些都是定義:

trait FooTrait 

case class Bar() extends FooTrait 

case class Bar() extends FooTrait 

這裏是我的宏,得到下FOO擴展FooTrait爲類的所有類型的符號:

def allTypeSymbols_impl[T: c.WeakTypeTag](c: Context)(packageName: c.Expr[String]) = { 
    import c.universe._ 

    // Get package name from the expression tree 
    val Literal(Constant(name: String)) = packageName.tree 

    // Get all classes under given package name 
    val pkg = c.mirror.staticPackage(name) 

    // Obtain type symbols for the classes - implementation omitted 
    val types = getTypeSymbols(c.universe)(List(pkg)) 

    // Apply method for List. For easy readability in later applications 
    val listApply = Select(reify(List).tree, newTermName("apply")) 

    val result = types.map { 
    t => 
     val typeName = c.Expr[TypeSymbol](Ident(t)) 
     println(s"Typename: $typeName, $t, ${t.toType}") 

     reify(typeName.splice).tree 
    } 

    println(s"RESULT: ${showRaw(result)}") 

    c.Expr[List[reflect.runtime.universe.TypeSymbol]](Apply(listApply, result.toList)) 
} 

第一println打印:

Typename: Expr[c.universe.TypeSymbol](Bar), class Bar, foo.Bar 
Typename: Expr[c.universe.TypeSymbol](Baz), class Baz, foo.Baz 

第二個打印:

RESULT: List(Ident(foo.Bar), Ident(foo.Baz)) 

但我收到此錯誤信息:

[error] no type parameters for method any2ArrowAssoc: (x: A)ArrowAssoc[A] exist so that it can be applied to arguments (<notype>) 
[error] --- because --- 
[error] argument expression's type is not compatible with formal parameter type; 
[error] found : <notype> 
[error] required: ?A 
[error] Note that <none> extends Any, not AnyRef. 
[error] Such types can participate in value classes, but instances 
[error] cannot appear in singleton types or in reference comparisons. 

我應該怎麼做做這個工作?我懷疑我必須寫點別的東西而不是Ident,但我無法弄清楚什麼。

使用Scala 2.10.2。

在此先感謝!

回答

7

你必須使用reifyType在運行宇宙創造反射假象:

import scala.language.experimental.macros 
import scala.reflect.macros.Context 

object PackageMacros { 
    def allTypeSymbols[T](packageName: String) = macro allTypeSymbols_impl[T] 

    def allTypeSymbols_impl[T: c.WeakTypeTag](c: Context)(
    packageName: c.Expr[String] 
) = { 
    import c.universe._ 

    val pkg = packageName.tree match { 
     case Literal(Constant(name: String)) => c.mirror.staticPackage(name) 
    } 

    val types = pkg.typeSignature.members.collect { 
     case sym: ClassSymbol => 
     c.reifyType(treeBuild.mkRuntimeUniverseRef, EmptyTree, sym.toType) 
    }.toList 

    val listApply = Select(reify(List).tree, newTermName("apply")) 

    c.Expr[List[Any]](Apply(listApply, types)) 
    } 
} 

這會給你一個類型的變量,而不是符號列表,但你可以很容易地得到符號,無論是像這樣:

scala> PackageMacros.allTypeSymbols("foo").map(_.tpe.typeSymbol) foreach println 
class Baz$ 
class Bar 
class Baz 
trait FooTrait 
class Bar$ 

或者在宏本身。

+0

再次感謝特拉維斯!我將它們轉換爲宏中的TypeSymbol。我在案例類中聲明的'val'字段中創建了一個'Map',但在嘗試訪問它時遇到此錯誤:http://pastebin.com/8dHDRMYy 我應該放鬆一下類型的要求,並像你一樣去「任何」? 另外,我將如何爲MethodSymbols執行此操作?有沒有辦法? – Emre