开发者

Is it possible to recover the name of the function from within the function in scala?

I'd like to do something like

def getMeASammy() {println "getMeASammy"}
def getMeAD开发者_运维问答rink() {println "getMeADrink"}
def getMeASub() {println "getMeASub"}

But, I don't want to explicitly type out the name of the function.


scala> def currentMethodName() : String = Thread.currentThread.getStackTrace()(2).getMethodName
currentMethodName: ()String

scala> def getMeASammy() = { println(currentMethodName()) }
getMeASammy: ()Unit

scala> getMeASammy()
getMeASammy


I wrote a simple library, which is using a macro to get the name of the function. It might be a more elegant solution than using Thread.currentThread.getStackTrace()(2).getMethodName if you don't mind additional dependency:

libraryDependencies += "com.github.katlasik" %% "functionmeta" % "0.2.3" % "provided"
import io.functionmeta._

def getMeASammy() {
   println(functionName) //will print "getMeASammy"
}


It's somewhat revolting, but the only supported way to get the name of the current method from the JVM is to create an exception (but not throw it), and then read the method name out of the exception's stack trace.

def methodName:String= new Exception().getStackTrace().apply(1).getMethodName()


Nameof does exactly this, and at compile time, so there are no creating exceptions, inspecting the stack trace and other reflection-ish overhead.

Relevant example from nameof readme:

import com.github.dwickern.macros.NameOf._

  def startCalculation(value: Int): Unit = {
    println(s"Entered ${nameOf(startCalculation _)}")
  }

  // compiles to:

  def startCalculation(value: Int): Unit = {
    println(s"Entered startCalculation")
  }

In your case

def getMeASammy() = println(nameOf(getMeASammy _))

Also, nameOf works with classes, class attributes, types, etc.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜