Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala IDE警告:“;可转换为方法值的匿名函数;_Scala_Methods_Alias_Anonymous Function - Fatal编程技术网

Scala IDE警告:“;可转换为方法值的匿名函数;

Scala IDE警告:“;可转换为方法值的匿名函数;,scala,methods,alias,anonymous-function,Scala,Methods,Alias,Anonymous Function,假设我想为一个方法创建一个别名: def foo = bar(_) 这将警告 可转换为方法值的匿名函数 我不太确定这意味着什么,因为当我尝试这可能意味着什么时: def foo = bar 我犯了一个错误 缺少方法栏的参数(a:a) 无法解析具有此签名的引用栏 首先,如果要为方法创建“别名”,就足够了: scala> val foo = bar(_) //val instead of def, still warning from Idea foo: Int => Int = &

假设我想为一个方法创建一个别名:

def foo = bar(_)
这将警告

可转换为方法值的匿名函数

我不太确定这意味着什么,因为当我尝试这可能意味着什么时:

def foo = bar
我犯了一个错误

缺少方法栏的参数(a:a)

无法解析具有此签名的引用栏


首先,如果要为方法创建“别名”,就足够了:

scala> val foo = bar(_) //val instead of def, still warning from Idea
foo: Int => Int = <function1>
实际上,它不仅仅是别名-您的方法将转换为(eta扩展)。您不能只指定方法(编译时实体),因为编译器需要参数-您需要先将其转换为函数(使用下划线)。有时,当编译器需要一个函数时,它会自动完成:

scala> val foo: Int => Int = bar
foo: Int => Int = <function1>

您必须指定
bar(,)
,因为有两个参数。

这是IntelliJ的建议。如果您在建议中单击“更多”,您将看到以下内容(抱歉,这是图形,我无法复制文本):

您可以忽略或使用
bar
而不是
bar(

scala> val foo: Int => Int = bar
foo: Int => Int = <function1>
scala> def bar(a: Int, b: Int) = a
bar: (a: Int, b: Int)Int

scala> def foo = bar _
foo: (Int, Int) => Int

scala> def foo = bar(_)
<console>:8: error: missing parameter type for expanded function ((x$1) => bar(x$1))
       def foo = bar(_)
                     ^
<console>:8: error: not enough arguments for method bar: (a: Int, b: Int)Int.
Unspecified value parameter b.
       def foo = bar(_)
                    ^

scala> def foo = bar(_, _)
foo: (Int, Int) => Int