简体   繁体   English

scala函数文字中的`=>`

[英]`=>` in scala function literal

What does the second => mean in the following function literal ? 在下面的函数文字中,second =>是什么意思?

val result: () => Int = () => throw new RuntimeException
result: () => Int = <function0>

In type declaration val result: () => Int it is just an easy way to declare a function type: 在类型声明中val result: () => Int它只是一种声明函数类型的简单方法:

() => Int is the same as Function0[Int] () => IntFunction0[Int]相同

Here () => throw new RuntimeException it's a function declaration and => separates arguments from body. Here () => throw new RuntimeException它是一个函数声明, =>将参数与body分开。 Therefore it's declaration of an anonymous function with no arguments and body throw new RuntimeException . 因此,它声明了一个没有参数的匿名函数,并且正文throw new RuntimeException It's equivalent to: 它相当于:

def f() = throw new RuntimeException

First => means that type of val is function which apply no parameters and return Int . First =>表示val类型是不应用参数并返回Int function

Second => is divider between parameter list () and function's body throw new RuntimeException Second =>是参数list ()和函数体之间的分隔符throw new RuntimeException

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM