New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Under -Xsource:3
, warn when eta-expanding SAMs
#8941
Conversation
I've never seen anyone add |
But I have seen a lot of code use this feature, perhaps courtesy of Intellij inspection suggesting it. |
Or maybe I got this wrong - is this about SAM without |
Hey, @joroKr21. Yeah, that's right: this is only about eta-expanding methods where a SAM is expected. I don't know the history around |
Dotty emits the following: -- Warning: target/tests/EtaX/EtaX.meth1/EtaX.meth1/EtaX.meth1.01.scala:10:24 -- 10 | val t5b: Sam1S = meth1 // ok, but warning | ^^^^^ |method meth1 is eta-expanded even though Sam1S does not have the @FunctionalInterface annotation. 1 warning found (given the following source:) trait Sam1S { def apply(x: Any): Any } class Test { def meth1(x: Any) = "" val t5b: Sam1S = meth1 // ok, but warning } So it makes sense for Scala 2.13 to do the same, under `-Xsource:3`. Now it actually also emits under `-source 3.0-migration` (which used to be called `-language:Scala2Compat`), so there's an argument that it should be on by default in 2.13... But let's do `-Xsource:3` at the very least.
971c900
to
1666ac1
Compare
I believe we'll keep the current behavior forever, the warning is there to prevent confusion and is easily solvable by making the eta-expansion explicit, see discussion in scala/scala3#4364 |
Are you saying the "write out the equivalent function literal" doesn't convey "explicit expansion" and/or that it's too late in the message? I'd be happy if we were to tweak the messaging. Want to send a PR with what you have in mind would work better? |
@dwijnand The former, "write out the equivalent function literal" is extremely heavy and doesn't give any hint that appending underscore
I am strictly against this warning in its entirety, both in Scala 2 and in Dotty, i'll give an example below why. This particular change makes I use eta-SAM syntax mostly to concisely implement type classes, e.g.: trait Encoder[A] {
def encode(a: A): Json
}
object Encoder {
implicit val jsonEncoder: Encoder[Json] = identity
} I don't find the linter complaints very relevant in this case, neither do i find the original example in scala/scala3#4364 (comment) surprising or justifying the warning, so I'm simply opposed to the warning. |
-Xsource:3
, warn when eta-expanding SAMs
Yep, these are back to error again. |
Dotty emits the following:
(given the following source:)
So it makes sense for Scala 2.13 to do the same, under
-Xsource:3
.Now it actually also emits in Dotty under
-source 3.0-migration
(which used to be called-language:Scala2Compat
), so there's an argument that it should be on by default in 2.13... But let's do-Xsource:3
at the very least.