有没有一种方法可以获得scala中对象方法中代码的解析树?据我所知,对于作为参数传递给具体化方法的表达式,这是可能的,如下所示。。。
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)
}
}
如果你的问题是是否可以将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