There are times when compiler is not expecting a function so it will not automatically convert the method into a function. For example, let’s revisit our method f2() and try to define a corresponding function with same functionality:
def f2() = "foo"
val f2fun = f2 // f2fun is a string, not a function!
What happened? If you remember that stupid confusing automatic conversion from earlier, you will recall that it is allowed to invoke a parameterless method (that is, an empty-parenthesised method) without the parenthesis. OK, we agreed we would avoid doing this deliberately in our code in order to avoid confusion, but what do we do about the existing convention in the compiler that interprets our f2 as f2()? How do we turn the method f2() into a function value f2?
Well, there are two things we can do:
explicitly declare the type of value to be a function
treat the method as a partially applied function
def f2() = "foo"
val f2fun1 = f2 // f2fun is a string, not a function
val f2fun2: () => String = f2 // however, this works!
val f2fun3 = f2 _ // this too!