-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Need An Error Message for Overloading Type-Mismatch #11899
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 wonder if the |
I confirmed that this happens on latest master as well. |
I am not sure what should be done here. The second match is not applicable, but the first one is. So Scala 3's behavior is as we would expect. |
We should check what Scala2 does. Could it be the case that there is hardcoded behaviour to prefer the |
But then why finish with a type error? |
@odersky For Scala code it makes sense to pick the generic overload, but that is never what a user wants with a Java library. Since Java's functional interfaces are now quite prevalent in APIs, I wonder how often this issue manifests, besides Vertx. One thing off the top of my head would be case class Pair(s: String, i: Int)
val l = new java.util.ArrayList[Pair]()
l.add(Pair("b", 1)); l.add(Pair("a", 5)); l.add(Pair("b", 0))
l.sort(java.util.Comparator.comparing(p => p.s)) With exception
The Java signature of static <T, U extends Comparable<? super U>> Comparator<T>
comparing(Function<? super T, ? extends U> keyExtractor); This code compiles & runs in Scala 2.13.4. Should I file a new issue, or maybe change the title to 'Java Function Compat Problems'? |
I think it is fine to keep it on this issue. It would be good if someone figured out what Scala 2.13's algorithm for treating these things is. |
Let's keep separate issues with separate problem. I'm going to close this issue because as far as I can tell the behavior of Scala 3 with these map overloads is perfectly correct (and I don't even see what special case we could add here that wouldn't break legitimate code), the fact that Scala 2 happens to emit an error seems more like a bug than an intentional design decision. IMO, the only issue here is an unfortunate API design choice in Vertex. |
Applicability testing as used in overloading resolution now considers a SAM conversion only if the argument is a lambda (possibly an eta-converted one). This matches exactly what normal adaptation does. It turns scala#11899 into a pos test (now part of i11938.scala). That is Scala 3 will fallback to a generic alternative where Scala 2 failed with a type error.
Uh oh!
There was an error while loading. Please reload this page.
Compiler version
3.0.0-RC1
EDIT: happens on latest
master
as well.Minimized code
Output
(does not matter if using def or lambda syntax)
Expectation
In Scala 2, the same code will not compile . It reports:
Vertx's Future has two such map methods, for example. As a feature request, it would be very nice to have special handling for
java.util.function
interfaces sof.map(fn: Function[Int, String])
is not required.The text was updated successfully, but these errors were encountered: