RFR: CSR - JDK-8203428 Predicate::not

Remi Forax forax at univ-mlv.fr
Fri May 18 20:01:53 UTC 2018


Hi Jim,
here is my review,
the target should use a wildcard,
  static <T> Predicate<T> not(Predicate<? super T> target) {
    return ...
  }

Due to a limitation of the inference, 'return target.negate()' will not compile.

One may think that, we should write 
  static <T> Predicate<T> not(Predicate<? super T> target) {
    return t -> !target.test(t);
  }
but if the class of 'target' override negate(), we will not call it.

So the right code seems to be
  @SuppressWarnings("unchecked")  // Predicate is contra-variant
  static <T> Predicate<T> not(Predicate<? super T> target) {
    return (Predicate<T>)target.negate();
  }

at least until JEP 300 [1] is implemented.

regards,
Rémi
  
[1] https://bugs.openjdk.java.net/browse/JDK-8043488

----- Mail original -----
> De: "Jim Laskey" <james.laskey at oracle.com>
> À: "core-libs-dev" <core-libs-dev at openjdk.java.net>
> Envoyé: Vendredi 18 Mai 2018 18:35:24
> Objet: RFR:  CSR - JDK-8203428 Predicate::not

> Introduce a new static method Predicate::not which will allow developers to
> negate predicate lambdas trivially.
> 
> 
> csr: https://bugs.openjdk.java.net/browse/JDK-8203428


More information about the core-libs-dev mailing list