I'm confused about the first line of code.
And what's the difference between the first line and the second line?
val cond: (Int, Int) => Boolean = (...) //confused
val cond = (x: Int, y: Int) => x > y //anonymous function
It can be a bit daunting at first, but all Scala declarations are the same shape:
val <name>[: <type>] = <value>
If the type
is not there the compiler will set it to the type of the value
So the first case breaks down like this:
name
is cond
type
is (Int, Int) => Boolean
value
is (...)
The second case breaks down like this:
name
is cond
value
is (x: Int, y: Int) => x > y
type
is inferred to be (Int, Int) => Boolean
In both cases cond
is a function that takes two Int
s and returns a Boolean
.
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