是否有可能获得scala中方法的解析树?

时间:2013-07-03 22:13:00

标签: scala reflection

有没有办法获得scala中对象方法中找到的代码的解析树?据我了解,这可以用于作为参数传递给reify方法的表达式,如下所示......

    import scala.reflect.runtime.universe._
    //I am able to retrieve a scala.reflect.api.Trees$Tree for this block of code...
    val i=1
    val tree1=reify{
        val b=i*3
        println("b was "+b)
    }.tree
    println("tree1->"+tree1)

    //Is it possible to obtain a tree for the block of code encapsulated in fooTest?
    object foo{
        def fooTest(i:Int)={
            val b=i*3
            println("b was "+b)
        }
    }

1 个答案:

答案 0 :(得分:1)

如果您的问题是您是否可以将Scala object中定义的方法(最接近“静态方法”)放到函数中,答案是肯定的:

object O {
  def oMethod(i: Int): String = i.toString
  val oFunction = oMethod _
}

scala> O.oMethod(23)
res0: String = 23

scala> O.oFunction(23)
res1: String = 23