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

StackOverflow https://stackoverflow.com/questions/3105002

  •  29-09-2019
  •  | 
  •  

Question

I'd like to do something like

def getMeASammy() {println "getMeASammy"}
def getMeADrink() {println "getMeADrink"}
def getMeASub() {println "getMeASub"}

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

Was it helpful?

Solution

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

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

scala> getMeASammy()
getMeASammy

OTHER TIPS

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

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top