一般来说,我想了解Scala如何推断参数为单元类型参数的参数,而其调用时省略了()
。需要参数为单元类型的函数
在this question给出一个例子:
val fun1: Unit => Int = u => 5
它可以通过以下方式来调用:
fun1()
这怎么编?是否将()
推断为参数而不是函数调用,即作为fun1(())
中的内部圆括号?
@ThaDon问了同样的问题,没有提供答案。
更进一步,我发现,这是行不通的:
scala> def unit =()
scala> fun1 unit
<console>:12: error: value unit is not a member of Unit => Int
fun1 unit
^
但这:
scala> fun1()
warning: there was one deprecation warning; re-run with -deprecation for details
res5: Int = 5
这
scala> fun1(unit)
res3: Int = 5
都工作。
我错过了什么fun1 unit
?编译器如何解析这段代码?
谢谢!我没有意识到在2.11.x中插入'()'参数的弃用,但我认为这是一个很好的改变! –