-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Resolve how to handle errors of closures taking by-name arguments with wildcard types #16789
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
Yes, I think excluding Java types is worth trying. Capture conversion is a core element of Java type checking, so we might want to trade off soundness there. Not sure whether capture conversion in Java itself is sound, the examples seem to indicate that it probably isn't. |
Indeed, Java isn't sound here either: interface Magic<S> {
S init();
String step(S s);
} class IntMagic implements Magic<Integer> {
public Integer init() { return 0; }
public String step(Integer s) { return "" + (s - 1); }
} class StrMagic implements Magic<String> {
public String init() { return "hi"; }
public String step(String s) { return new StringBuilder(s).reverse().toString(); }
} import java.util.*;
import java.util.function.*;
class Main {
static <T> String onestep(Supplier<Magic<T>> m) {
return m.get().step(m.get().init());
}
public static void main(String... args) {
Iterator<Magic<?>> iter = new Iterator<Magic<?>>() {
private Boolean toggle = false;
public boolean hasNext() { return true; }
public Magic<?> next() {
Magic<?> next = toggle ? new IntMagic() : new StrMagic();
toggle = toggle ? false : true;
return next;
}
};
onestep(() -> iter.next());
}
}
|
@dwijnand That's good to know! I wonder whether something on that has been published yet. |
fyi @S11001001, whose bug report (scala/bug#9419) and blog post (https://typelevel.org/blog/2015/07/30/values-never-change-types.html) are where we got this from |
Case 1 (zio-http) and Case 2 (specs2) are pretty different from each other — or at least, they seem so to us at the moment. Dale thinks Case 2 might be fixable by reordering the sequence of events in typer. If we can reverse the order so that wildcard capture isn't tried until after extension methods are resolved, then the problem might go away in this case. And it seems like an appealing change, since we do think of wildcard capture as a questionable last resort, and in Case 2 it isn't actually needed at all. As for Case 1, we think we could advise the library author that they ought to change the signature of their method to use We've been discussing this back and forth for 2+ hours now, so I haven't been able to write down all of our thinking, but I wanted to at least get this summary down. |
Just opening the barn door for Java-defined types remains an option, but I don't find it very satisfactory, which is why we've been trying so hard to find alternatives. I feel like it simultaneously opens the barn door too wide (we don't need to exempt all Java types from this checking) and not wide enough (these two use cases happen to involve a Java-defined type, but I'm not convinced that it's a necessary condition). |
his PR takes a different approach — see #16799 |
Compiler version
3.3.1-RC1-bin-20230129-b8ad7b1-NIGHTLY
Related to closures with by-name arguments change b439deb
Durated the meeting it was mentioned, that maybe we should treat Java types differently
Minimized code
Based on the results of OpenCB build (links to build logs below)
Case 1
Based on compilation issue in zio/zio-http
Output
Case 2
When used with Specs2 4.x in tests of reactivemongo/reactivemongo-bson and playframework/play-ws
Output
Expectation
The text was updated successfully, but these errors were encountered: