使用Scala REPL,我可以得到
的类型scala> def tryit(thing: Null): Unit = { println("That worked!"); }
tryit: (thing: Null)Unit
然后,如何在REPL之外的Scala源中获取相同的信息?
我试过了getClass
,但是我收到了一个错误。
<console>:12: error: missing arguments for method tryit;
follow this method with `_' if you want to treat it as a partially applied function
tryit.getClass
^
从另一个堆栈溢出问题,我可以得到这个代码。
// http://stackoverflow.com/questions/11486298/how-can-i-get-the-functionality-of-the-scala-repl-type-command-in-my-scala-prog
import reflect.runtime.universe._
implicit class ColonTypeExtender [T : TypeTag] (x : T) {
def colonType = typeOf[T].toString
}
object Main extends App {
// Null is a type, and null is a value
def tryit(thing: Null): Unit = { println("That worked!"); }
println((tryit _).colonType)
tryit(null)
}
运行代码将显示我期望的结果。
Null => Unit