The conversion of methods into functions (eta-expansion) has been improved and happens automatically for methods with one or more parameters.
def m(x: Boolean, y: String)(z: Int): List[Int]
val f1 = m
val f2 = m(true, "abc")
This creates two function values:
f1: (Boolean, String) => Int => List[Int]
f2: Int => List[Int]
The syntax m _
is no longer needed and will be deprecated in the future.
Automatic eta-expansion and nullary methods
Automatic eta expansion does not apply to “nullary” methods that take an empty parameter list.
def next(): T
Given a simple reference to next
does not auto-convert to a function.
One has to write explicitly () => next()
to achieve that
Once again since the _
is going to be deprecated it’s better to write it this way
rather than next _
.
The reason for excluding nullary methods from automatic eta expansion
is that Scala implicitly inserts the ()
argument, which would
conflict with eta expansion. Automatic ()
insertion is
limited in Dotty, but the fundamental ambiguity
remains.
Motivation
In Scala, previously, a method reference m
was converted to a function value only if the expected type was a function type. If that was not the case, one had to write m _
to force the conversion. For methods with one or more parameters, this restriction has now been dropped reducing the syntactic overhead; previously a responsibility of the programmer. The syntax m _
is no longer needed and will be deprecated in the future.
This proposal is open for discussion in the community and will be discussed in our next SIP meeting, where we’ll take all your feedback into account for the approval/dismissal of this feature.
Related
This SIP proposal is related to the proposal to remote auto-application from the language. For example. A nullary method in this SIP, like next()
above, cannot be converted with a simple reference to next
. The discussion to remote auto-application from the language is happening here: Proposal to remove auto application from the language