=>
在下面的函数文字中,第二个含义是什么?
val result: () => Int = () => throw new RuntimeException
result: () => Int = <function0>
Run Code Online (Sandbox Code Playgroud)
在类型声明中,val result: () => Int
它只是一种声明函数类型的简单方法:
() => Int
是相同的 Function0[Int]
这() => throw new RuntimeException
是一个函数声明,=>
并将参数与body分开.因此,它声明了一个没有参数和正文的匿名函数 throw new RuntimeException
.它相当于:
def f() = throw new RuntimeException
Run Code Online (Sandbox Code Playgroud)