You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Eta-expand 0-arity method if expected type is Function0
Reverse course from the deprecation introduced in 2.12.
(4.3) condition for eta-expansion by -Xsource level:
- until 2.13:
- for arity > 0: function or sam type is expected
- for arity == 0: Function0 is expected -- SAM types do not eta-expand
because it could be an accidental SAM scala/bug#9489
- 2.14:
- for arity > 0: unconditional
- for arity == 0: a function-ish type of arity 0 is expected (including SAM)
- 3.0: auto-application takes precedence, but otherwise we always eta-expand
warnings:
- 2.12: eta-expansion of zero-arg methods was deprecated (scala/bug#7187)
- 2.13: deprecation dropped in favor of setting the scene for uniform eta-expansion in 3.0
- 2.14: expected type is a SAM that is not annotated with `@FunctionalInterface`
(4.2) condition for auto-application by -Xsource level:
- until 2.14: none (assuming condition for (4.3) was not met)
- in 3.0: `meth.isJavaDefined`
// (4.2) condition for auto-application by -Xsource level
950
+
//
951
+
// until 2.14: none (assuming condition for (4.3) was not met)
952
+
// in 3.0: `meth.isJavaDefined`
953
+
defcanAutoApply:Boolean=
954
+
if (sourceLevel3) meth.isJavaDefined
955
+
else {
956
+
if (sourceLevel2_14 &&!meth.isJavaDefined)
957
+
context.deprecationWarning(tree.pos, NoSymbol, s"Auto-application to `()` is deprecated. Write ${Apply(warnTree, Nil)} to invoke method ${meth.decodedName},"+
958
+
s"or remove the empty argument list from its definition (Java-defined methods are exempt).\n"+
959
+
s"In Scala 3, an unapplied method will be eta-expanded, as in: ${Function(Nil, Apply(warnTree, Nil))}.", "2.14.0")
960
+
true
961
+
}
962
+
963
+
if (!meth.isConstructor && canEtaExpand) { if (!expectingFunctionOfArity) warnEtaSam(); typedEtaExpansion(tree, mode, pt) }
val t2: () => Any = m2 // error, no eta-expansion of zero-args methods
6
+
t7187-2.14.scala:14: warning: An unapplied 0-arity method was eta-expanded (due to the expected type () => Any), rather than applied to `()`.
7
+
Write m2() to invoke method m2, or change the expected type.
8
+
val t2: () => Any = m2 // eta-expanded with lint warning
9
+
^
10
+
t7187-2.14.scala:15: warning: An unapplied 0-arity method was eta-expanded (due to the expected type AcciSamZero, which is SAM-equivalent to () => Int), rather than applied to `()`.
11
+
Write m2() to invoke method m2, or change the expected type.
12
+
val t2AcciSam: AcciSamZero = m2 // eta-expanded with lint warning + sam warning
13
+
^
14
+
t7187-2.14.scala:15: warning: Eta-expansion performed to meet expected type AcciSamZero, which is SAM-equivalent to () => Int,
15
+
even though trait AcciSamZero is not annotated with `@FunctionalInterface`; add annotation to suppress warning.
16
+
val t2AcciSam: AcciSamZero = m2 // eta-expanded with lint warning + sam warning
17
+
^
18
+
t7187-2.14.scala:16: warning: An unapplied 0-arity method was eta-expanded (due to the expected type SamZero, which is SAM-equivalent to () => Int), rather than applied to `()`.
19
+
Write m2() to invoke method m2, or change the expected type.
20
+
val t2Sam: SamZero = m2 // eta-expanded with lint warning
0 commit comments