在 Scala REPL 中可以找到值类型:
scala> val x = 1
x: Int = 1
scala> :t x
Int
然而 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
^
如何在 Scala REPL 中查找函数类型?
遵循建议会效果很好:
:t inc _
Int => Int
更详细地说,这是必要的,因为 Scala 维护了“方法”和“函数”之间的区别,“方法”在 JVM 中具有本机支持,但不是一流的,而“函数”则被视为FunctionX
并被 JVM 视为对象。使用尾随下划线将前者转换为后者。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)