Scala REPL:如何查找函数类型?

时间:2013-03-25 09:38:14

标签: function scala types read-eval-print-loop

在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中找到函数类型?

2 个答案:

答案 0 :(得分:23)

遵循这个建议会很有效:

:t inc _
Int => Int

为了提供更多细节,这是必要的原因是Scala在“方法”之间保持区别,“方法”在JVM中具有本机支持但不是第一类,而“函数”则被视为实例是FunctionX并被JVM视为对象。使用尾随下划线将前者转换为后者。

答案 1 :(得分:2)

您可以写下方法的名称,然后按标签。

Stream.fill<tab>

给你:

def fill[A](n1: Int,n2: Int,n3: Int)(elem: => A): 
scala.collection.immutable.Stream[scala.collection.immutable.Stream[scala.collection.immutable.Stream[A]]]
def fill[A](n1: Int,n2: Int,n3: Int,n4: Int)(elem: => A): scala.collection.immutable.Stream[scala.collection.immutable.Stream[scala.collection.immutable.Stream[scala.collection.immutable.Stream[A]]]]
def fill[A](n1: Int,n2: Int)(elem: => A): scala.collection.immutable.Stream[scala.collection.immutable.Stream[A]]
def fill[A](n1: Int,n2: Int,n3: Int,n4: Int,n5: Int)(elem: => A): scala.collection.immutable.Stream[scala.collection.immutable.Stream[scala.collection.immutable.Stream[scala.collection.immutable.Stream[scala.collection.immutable.Stream[A]]]]]
override def fill[A](n: Int)(elem: => A): scala.collection.immutable.Stream[A]