scala> val f = (x:Int) => x+1
f: Int => Int = <function1>
scala> println(f)
<function1>
是否可以在程序(而不是控制台)中打印f: Int => Int = <function1>
字符串?我主要需要Int => Int
部分进行测试。
答案 0 :(得分:2)
也许:类型就是你想要的
scala> val f = (x:Int) => x+1
f: Int => Int = <function1>
scala> :type f
Int => Int
既然你想在程序中获得它,那么TypeTag可能是一个选择
scala> :paste
// Entering paste mode (ctrl-D to finish)
import scala.reflect.runtime.{ universe => ru }
import scala.reflect.runtime.universe.{ TypeTag, typeTag }
def getTypeTag[T: TypeTag](o: T) = typeTag[T].tpe
// Exiting paste mode, now interpreting.
scala> val f = (x:Int) => x+1
f: Int => Int = <function1>
scala> getTypeTag(f)
res0: reflect.runtime.universe.Type = Int => Int
scala> getTypeTag(f).toString
res1: String = Int => Int
答案 1 :(得分:0)
您可以通过
获得f.getClass