1

有没有办法在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)
        }
    }
4

1 回答 1

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
于 2013-07-04T01:45:06.413 回答