Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 getMeADrink() {println "getMeADrink"} def getMeASub() {println "getMeASub"} 

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

like image 260
Jija Avatar asked Jun 23 '10 19:06

Jija


People also ask

What does => mean in Scala?

=> is the "function arrow". It is used both in function type signatures as well as anonymous function terms. () => Unit is a shorthand for Function0[Unit] , which is the type of functions which take no arguments and return nothing useful (like void in other languages).

How do you pass a value to a function in Scala?

One function can be passed to another function as a function argument (i.e., a function input parameter). The definition of the function that can be passed in as defined with syntax that looks like this: "f: Int > Int" , or this: "callback: () > Unit" .


1 Answers

scala> def currentMethodName() : String = Thread.currentThread.getStackTrace()(2).getMethodName currentMethodName: ()String  scala> def getMeASammy() = { println(currentMethodName()) } getMeASammy: ()Unit  scala> getMeASammy() getMeASammy 
like image 124
Richard Fearn Avatar answered Sep 28 '22 03:09

Richard Fearn