I've replicated an exception thrown when attempting to use type variable type intersection to capture lambdas; specifically, to replicate, I attempted to catch a lambda as F
where <A, F extends Consumer<A> & Serializable>
:
public class Consumers {
public static <A, F extends Consumer<A> & Serializable>
Consumer<A> serializable(F action) {
return action;
}
public static <A> Consumer<A> vary(Consumer<? super A> action) {...}
private static final Consumer<Object> DOES_NOTHING =
serializable(a -> {});
public static <A> Consumer<A> doesNothing() {
return vary(DOES_NOTHING);
}
...
}
Consumer<String> action = Consumers.doesNothing(); // throws class cast exception
The following is an example of the exception thrown:
java.lang.ClassCastException: [insert lambda name] cannot be cast to java.util.function.Consumer
I'm able to use Consumers.serializable
with local or instance variables; this exception is thrown when trying to initialize static variables.
Is this correct behavior? I'm using the latest Eclipse Oxygen, JDK u112.
This should not be allowed:
javac
complains:It is actually an Eclipse compiler bug, fixed in 4.6 M6 (Neon), and it has already been noticed in this other question.