is there a way where I can get the string representation of a function?
val f = List(1, 2, 3, 4, 66, 11).foldLeft(55)_
f
is a function of type ((Int, Int) => Int) => Int
, and that would be the representation I am looking for, but I can't find it anywhere.
the toString
method was my first try, of course, but all it returns is <function1>
. scala REPL does it right, and the Documentation too. There must be a way?
Regards.
When the type is known at compile time, you can use Scalas TypeTag
:
scala> import reflect.runtime.universe._
import reflect.runtime.universe._
scala> def typeRep[A : TypeTag](a: A) = typeOf[A]
typeRep: [A](a: A)(implicit evidence$1: reflect.runtime.universe.TypeTag[A])reflect.runtime.universe.Type
scala> val f = List(1, 2, 3, 4, 66, 11).foldLeft(55)_
f: ((Int, Int) => Int) => Int = <function1>
scala> typeRep(f)
res2: reflect.runtime.universe.Type = ((Int, Int) => Int) => Int
For a detailed description on what TypeTag
is doing see another answer.