A function is a Function, after all

In Scala a function is an object. This can be deduced from the existence of a set of traits called scala.Function, scala.Function1, … scala.Function22. The traits define the abstract method apply() (I’ve already written something about it) and some concrete methods. The use of this trait is like this:

object Main extends App {
   val succ = (x: Int) => x + 1
   val anonfun1 = new Function1[Int, Int] {
     def apply(x: Int): Int = x + 1
   }
   assert(succ(0) == anonfun1(0))
}

This example is taken Scala documentation. What can a function like anonfun1 be useful for? Well, the code shows by itself: succ(0) is equal to anonfun(0). The code of succ and anonfun1 is the same: it takes an Int argument and return an Int which is the argument plus one.