Scala REPL:如何查找函数类型? [英] Scala REPL: How to find function type?

查看:128
本文介绍了Scala REPL:如何查找函数类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Scala REPL中,可以找到值类型:

  scala> val x = 1 
x:Int = 1

scala> :tx
Int

然而Scala REPL并没有显示函数的类型信息: p>

  scala> def inc(x:Int)= x + 1 
inc:(x:Int)Int

scala> :t inc
< console>:9:错误:缺少方法inc的参数;
如果您想将其视为部分应用函数,请使用`_'此方法
inc
^
< console>:9:error:缺少method inc的参数;
如果您想将其视为部分应用的函数,则使用`_'跟随此方法
inc
^

如何在Scala REPL中查找函数类型?

解决方案

很好:

 :t inc _ 
Int =>为了给出更多的细节,这个必要的原因是Scala保持了'区域'和'区域'之间的区别,方法,它们在JVM中具有本地支持,但不是第一类,而'函数'被视为 FunctionX 的实例,并被JVM视为对象。使用尾部下划线将前者转换为后者。


In Scala REPL one can find value types:

    scala> val x = 1
    x: Int = 1

    scala> :t x
    Int

Yet Scala REPL does not show the type information for functions:

    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
          ^

How to find function type in Scala REPL ?

解决方案

Following the suggestion will work pretty well:

:t inc _
Int => Int

To give a bit more detail, the reason this is necessary is that Scala maintains a distinction between 'methods', which have native support in the JVM but which are not first class, and 'functions', which are treated as instances of FunctionX and seen as objects by the JVM. The use of the trailing underscore converts the former to the latter.

这篇关于Scala REPL:如何查找函数类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆