2013-07-15 103 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