What does the second => mean in the following function literal ?
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:
() => Int is the same as Function0[Int]
Here () => throw new RuntimeException it's a function declaration and => separates arguments from body. Therefore it's declaration of an anonymous function with no arguments and body 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.
Second => is divider between parameter list () and function's body throw new RuntimeException
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With