Take this as an introductory example:
def fun() { println("fun1 executed.") } val a1 = fun val a2: () => Unit = fun
Both lines are compiled and (thanks to type inference) look equivalent. However, a1 is of type Unit , and a2 is of type () => Unit ... How is this possible?
Since you do not explicitly specify type a1 , compilers interpret fun as calling a fun method of type Unit , so type a1 same as type fun . It also means that this line will print fun1.
However, a2 has an explicitly declared type () => Unit . The compiler helps you here, and he understands that since the context requires a function of the type () => Unit , and you have provided a method corresponding to this type, it should not call this method, but consider it as a function of the first class!
You are not doomed to explicitly specify type a1 . Saying:
val a1 = fun _
Now do you understand where your problem is?
source share