Il seguente codice si compila bene in IntelliJ ed Eclipse, ma il compilatore JDK 1.8.0_25 si lamenta. Innanzitutto, il codice.errato tipo restituito nell'espressione lambda
import java.util.function.Predicate;
public abstract class MyStream<E> {
static <T> MyStream<T> create() {
return null;
}
abstract MyStream<E> filter(MyPredicate<? super E> predicate);
public interface MyPredicate<T> extends Predicate<T> {
@Override
boolean test(T t);
}
public void demo() {
MyStream.<Boolean> create().filter(b -> b);
MyStream.<String> create().filter(s -> s != null);
}
}
L'uscita da javac 1.8.0_25 è:
MyStream.java:18: error: incompatible types: incompatible parameter types in lambda expression
MyStream.<Boolean> create().filter(b -> b);
^
MyStream.java:18: error: incompatible types: bad return type in lambda expression
MyStream.<Boolean> create().filter(b -> b);
^
? super Boolean cannot be converted to boolean
MyStream.java:19: error: bad operand types for binary operator '!='
MyStream.<String> create().filter(s -> s != null);
^
first type: ? super String
second type: <null>
MyStream.java:19: error: incompatible types: incompatible parameter types in lambda expression
MyStream.<String> create().filter(s -> s != null);
^
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
4 errors
Quando sostituisco ? super E
con semplicemente E
, JDK compila con successo.
Quando sostituisco filter(MyPredicate
con filter(Predicate
, JDK viene compilato correttamente.
Dato che funziona con JDK 1.8.0_60, ho il sospetto che si tratti di un errore del compilatore.
Eventuali dettagli su ciò che ha causato questo e quando è stato risolto?
sì, anch'io credo che sia un bug del compilatore.prova il filtro '((Booleano b) -> b))' ' – ZhongYu