Consider the following Scala code.
object Q{
trait C{
def f(x: Int) : Int
}
def applyTo3(c: C) = c.f(3)
def main(args: Array[String]) = println(applyTo3(x => x+1))
}
This looks like it shouldn't compile: the function applyTo3 expects an argument of type C, but instead is given an argument of type Int => Int. But in fact it does compile, and gives the result 4 when run. The function is somehow being converted to an object of type C, but I don't see how.
Gavin
As mentioned in the comments, this is lambda syntax for SAM (Single Abstract Method).
If we break out the argument to your function, we can see what this syntactic sugar does for us (I have also added a return type for
applyTo3):Here is more information on the language specifications for SAM conversion requirements: https://www.scala-lang.org/files/archive/spec/2.12/06-expressions.html#sam-conversion