hickin
hickin

Reputation: 68

Is it possible to obtain the parse tree to a method in scala?

Is there a way to obtain the parse tree to the code found within an object's method in scala? As I understand it, this is possible to do for an expression that is passed as an argument to the reify method as shown below...

    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)
        }
    }

Upvotes: 1

Views: 99

Answers (1)

Randall Schulz
Randall Schulz

Reputation: 26486

If your question is whether you can lift a method defined in a Scala object (the closest thing there is to a "static method") to a function, the answer is yes:

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

Upvotes: 1

Related Questions