-
Notifications
You must be signed in to change notification settings - Fork 1.1k
SAM types + Overloading + Eta-expansion #4364
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
Comments
I tried out some things. The problem is, if we allow that, the following also compiles:
|
I agree. This currently compiles: class Test {
def foo(x: Any): Unit = ()
def test = {
new java.io.ObjectOutputStream(foo)
}
}
|
@odersky I think this problem stems from the functional interface, not Eta-expansion. We should check the functional interface and trigger compiler warnings when the interface is not annotated with @FunctionalInterface. When the target interface is a Scala trait and it not annotated by @FunctionalInterface, we should let the compiler report an error |
This would add a lot of unnecessary warnings. Java doesn't emit any warning when using a SAM type that is not annotated with @FunctionalInterface. |
But Java's behavior creates a lot of unexpected functional interfaces, and in Java, lambda expressions and method reference expressions only be used in scenes where SAM conversion occurs without any confusion, different from Scala's lambda expression and eta-expansion. |
In fact, most of the commonly used functional interfaces in the JDK have been annotated as @FunctionalInterface. It may not be so common for users to actually disable warnings. |
I guess it worth doing the experiment and see how many warnings we get in our build and the community build |
On the one hand I agree Scalac's behavior is surprising here. OTOH, changing this adds an incompatibility, so we'd have to add the warnings at least to 2.14. Even if the impact were minor on our community build, we'd need to try this out on Scalac's one to gauge actual impact. The experiment might still help: if the impact is major on our build, we can't change it ourselves. |
Fix #4364: Try SAM type when no candidates are found
The following code snippet fails to compile with Dotty. (Works with scalac)
The text was updated successfully, but these errors were encountered: