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)
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`
TODO decide -- currently the condition is more involved to give slack to
Scala methods overriding Java-defined ones; I think we should resolve that
by introducing slack in overriding e.g. a Java-defined `def toString()`
by a Scala-defined `def toString`. This also works better for dealing
with accessors overriding Java-defined methods.
The current strategy in methodSig is problematic:
> // Add a () parameter section if this overrides some method with () parameters
> val vparamSymssOrEmptyParamsFromOverride =
This means an accessor that overrides a Java-defined method gets a
MethodType instead of a `NullaryMethodType`, which breaks lots of
assumptions about accessors
if (doIt &&!expectingFunctionOfArity) warnEtaSam()
947
+
948
+
doIt
907
949
}
908
-
// (4.3) eta-expand method value when function or sam type is expected (for experimentation, always eta-expand under 2.14 source level)
909
-
elseif (isFunctionProto(pt) || settings.isScala214) { // TODO: decide on `settings.isScala214`
910
-
if (settings.isScala212 && mt.params.isEmpty) // implies isFunctionType(pt)
911
-
currentRun.reporting.deprecationWarning(tree.pos, NoSymbol, "Eta-expansion of zero-argument methods is deprecated. "+
912
-
s"To avoid this warning, write ${Function(Nil, Apply(tree, Nil))}.", "2.12.0")
913
950
914
-
typedEtaExpansion(tree, mode, pt)
951
+
952
+
// (4.2) condition for auto-application by -Xsource level
953
+
//
954
+
// until 2.14: none (assuming condition for (4.3) was not met)
955
+
// in 3.0: `meth.isJavaDefined`
956
+
// (TODO decide -- currently the condition is more involved to give slack to Scala methods overriding Java-defined ones;
957
+
// I think we should resolve that by introducing slack in overriding e.g. a Java-defined `def toString()` by a Scala-defined `def toString`.
958
+
// This also works better for dealing with accessors overriding Java-defined methods. The current strategy in methodSig is problematic:
959
+
// > // Add a () parameter section if this overrides some method with () parameters
960
+
// > val vparamSymssOrEmptyParamsFromOverride =
961
+
// This means an accessor that overrides a Java-defined method gets a MethodType instead of a NullaryMethodType, which breaks lots of assumptions about accessors)
962
+
defcheckCanAutoApply():Boolean= {
963
+
if (sourceLevel2_14 &&!meth.isJavaDefined)
964
+
context.deprecationWarning(tree.pos, NoSymbol, s"Auto-application to `()` is deprecated. Write ${Apply(warnTree, Nil)} to invoke method ${meth.decodedName},"+
965
+
s"or remove the empty argument list from its definition (Java-defined methods are exempt).\n"+
966
+
s"In Scala 3, an unapplied method will be eta-expanded, as in: ${Function(Nil, Apply(warnTree, Nil))}.", "2.14.0")
967
+
true
915
968
}
916
-
else cantAdapt
969
+
970
+
if (!meth.isConstructor && checkCanEtaExpand()) 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