Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

val cond: (Int, Int) => Boolean = (...) what does this scala code mean? [closed]

Tags:

scala

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
like image 927
17hao Avatar asked Sep 17 '25 04:09

17hao


1 Answers

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:

  • The name is cond
  • The type is (Int, Int) => Boolean
  • The value is (...)

The second case breaks down like this:

  • The name is cond
  • The value is (x: Int, y: Int) => x > y
  • The type is inferred to be (Int, Int) => Boolean

In both cases cond is a function that takes two Ints and returns a Boolean.

like image 200
Tim Avatar answered Sep 19 '25 19:09

Tim