Ant*_*nin 10 types scala function read-eval-print-loop
在Scala REPL中,可以找到值类型:
scala> val x = 1
x: Int = 1
scala> :t x
Int
Run Code Online (Sandbox Code Playgroud)
但是Scala REPL没有显示函数的类型信息:
scala> def inc(x:Int) = x + 1
inc: (x: Int)Int
scala> :t inc
<console>:9: error: missing arguments for method inc;
follow this method with `_' if you want to treat it as a partially applied function
inc
^
<console>:9: error: missing arguments for method inc;
follow this method with `_' if you want to treat it as a partially applied function
inc
^
Run Code Online (Sandbox Code Playgroud)
如何在Scala REPL中找到函数类型?
Imp*_*ive 24
根据建议将很好地工作:
:t inc _
Int => Int
Run Code Online (Sandbox Code Playgroud)
为了给出更多细节,这是必要的原因是Scala保持了"方法"之间的区别,它们在JVM中具有本机支持但不是第一类,而"函数"则被视为实例FunctionX
和被视为实例作为JVM的对象.使用尾随下划线将前者转换为后者.