Lambda and wildcard
Tagir Valeev
amaembo at gmail.com
Tue Oct 7 16:12:22 UTC 2025
Hello!
I'm investigating a seemingly weird compilation case. Consider the
following Java interface:
import java.util.function.Supplier;
interface Main {
interface X<T> {
X<T> self();
}
static X<?> makeX() {return null;}
static <R> X<R> create(Supplier<? extends R> supplier) {return null;}
static X<X<?>> methodRef() {
return create(Main::makeX).self();
}
static X<X<?>> lambda() {
return create(() -> makeX()).self();
}
}
I expect that either both methods 'methodRef' and 'lambda' should be
compilable or both should be non-compilable. However, while 'methodRef'
compiles, 'lambda' is rejected by compiler (using javac build 25+36-3489):
Main.java:17: error: incompatible types: X<X<CAP#1>> cannot be converted to
X<X<?>>
return create(() -> makeX()).self();
^
where CAP#1 is a fresh type-variable:
CAP#1 extends Object from capture of ?
1 error
error: compilation failed
Could you please help me and clarify whether this is an expected behavior
or not?
With best regards,
Tagir Valeev
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/compiler-dev/attachments/20251007/c1dff985/attachment.htm>
More information about the compiler-dev
mailing list