scala3 icon indicating copy to clipboard operation
scala3 copied to clipboard

wrong `you can simply leave out the trailing _` message

Open xuwei-k opened this issue 6 months ago • 2 comments

Compiler version

  • 3.7.0
  • 3.7.1-RC2

Minimized example

Welcome to Scala 3.7.1-RC2 (21.0.7, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.
                                                                                                                                             
scala> trait A[B] { def f(x: String): B }
// defined trait A
                                                                                                                                             
scala> case class C(value: String)
// defined case class C
                                                                                                                                             
scala> val x: A[C] = C.apply _
1 warning found
-- Warning: --------------------------------------------------------------------
1 |val x: A[C] = C.apply _
  |              ^^^^^^^^^
  |              The syntax `<function> _` is no longer supported;
  |              you can simply leave out the trailing ` _`
val x: A[C] = Lambda/0x00000003015984c8@1d6a22dd
                                                                                                                                             
scala> val x: A[C] = C.apply
1 warning found
-- Warning: --------------------------------------------------------------------
1 |val x: A[C] = C.apply
  |              ^^^^^^^
  |method apply is eta-expanded even though A[C] does not have the @FunctionalInterface annotation.
val x: A[C] = Lambda/0x00000003015c1400@68a94e58

Why this Error/Warning was not helpful

compiler report another warning if remove _

xuwei-k avatar May 29 '25 04:05 xuwei-k

It's OK to get a second warning, but that message was changed in Scala 2 to be more instructional:

t11644a.scala:24: warning: Eta-expansion to expected type AcciSamOne, which is not a function type but is SAM-convertible to Int => Int.
trait AcciSamOne should be annotated with `@FunctionalInterface` if eta-expansion is desired.
Avoid eta-expansion by writing the function literal `((x: Int) => m3(x))` or `m3(_)`.
This warning can be filtered with `-Wconf:cat=lint-eta-sam`.
  val t3AcciSam: AcciSamOne = m3  // warn

https://github.com/scala/bug/issues/11644

That ticket links to the motivating Scala 3 discussions.

som-snytt avatar May 29 '25 08:05 som-snytt

More annoying is that -source:3.4-migration -rewrite produces C.apply.

Relatedly, the auto-insert says

8 |val ss = List("42").map(C)
  |                        ^
  |The method `apply` is inserted. The auto insertion will be deprecated, please write `C.apply` explicitly.

som-snytt avatar May 29 '25 08:05 som-snytt