Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

`=>` in scala function literal

Tags:

function

scala

What does the second => mean in the following function literal ?

val result: () => Int = () => throw new RuntimeException
result: () => Int = <function0>
like image 856
user2018791 Avatar asked Feb 27 '26 16:02

user2018791


2 Answers

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
like image 141
ka4eli Avatar answered Mar 01 '26 09:03

ka4eli


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

like image 43
Sergii Lagutin Avatar answered Mar 01 '26 08:03

Sergii Lagutin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!