Scala functional literals

It might be helpful to write out the full types of x and y like so

val x: (Int, Int) => (Int, Int) => Int = 
  (a: Int, b: Int) => (_: Int) + (_: Int)

val y: (Int, Int) => Int = 
  (_: Int) + (_: Int)

Here we see when x is applied to two arguments it returns yet another function of type

(Int, Int) => Int

Note that shorthand

(_: Int) + (_: Int)

is equivalent to

(a: Int, b: Int) => a + b

val x = (x:Int, y:Int) => (_:Int) + (_:Int)

Is equivalent to

val x = (x : Int, y : Int) => ((arg1:Int, arg2:Int) => (arg1:Int) + (arg1:Int))

While

val y = (_:Int) + (_:Int)

Is equivalent to

(x:Int, y:Int) => (x:Int) + (x:Int)

Tags:

Literals

Scala