-
Notifications
You must be signed in to change notification settings - Fork 357
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
Optional::map doesn't accept @Nullable #1633
Comments
Possibly related to #979. |
The same concern about java.util.Optional#orElseGet: it accepts Supplier returning null but CF is not. |
This is a limitation of the Checker Framework's implementation of type argument inference. We are working on implementing the better type argument inference. See #979. In the meantime, explicitly specify the type argument avoids the error. For example, the following code type checks.:
I'm closing this issue in favor of #979. |
Annotating of Optional::map can solve this problem:
A little bit harder problem with orElseGet:
solve the problem, but it always return
doesn't help at all |
I see; we can work around #979 in this case by adding annotations to Optional. I'm going to reopen this issue. |
Regarding Regarding // Test case for Issue 1633:
// https://github.com/typetools/checker-framework/issues/1633
// @below-java8-jdk-skip-test
import java.util.function.Supplier;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.checker.nullness.qual.PolyNull;
class Issue1633 {
void foo4nw(Optional<String> o, Supplier<@Nullable String> supplyNullable) {
@Nullable String str3 = o.orElseGet(supplyNullable);
}
void foo8nw(Optional<String> o, Supplier<@NonNull String> supplyNonNull) {
@NonNull String str3 = o.orElseGet(supplyNonNull);
}
}
// From the JDK
final @NonNull class Optional<T extends @Nullable Object> {
/** If non-null, the value; if null, indicates no value is present */
private final @Nullable T value = null;
public @PolyNull T orElseGet(Supplier<? extends @PolyNull T> other) {
// The commented-out line fails to typecheck due to issue #979
// return value != null ? value : other.get();
if (value != null) {
return value;
} else {
return other.get();
}
}
} |
Note that the annotated JDK currently doesn't contain methods that require Java 8. Therefore, these changes are not helpful to clients until that issue is resolved.
Hello, thank you for attention.
Both of them produce compile-time error but should not. Could you please explain a little bit more about writing my stub: Am I wrong, but I suppose jd8.jar is intended to use in java 8 only. How could it affect java 7? |
Thanks for the additional test cases. I have added them, but the problems with Regarding JDK7 vs JDK8, you are correct. The commented-out text in |
Hello, |
For me it looks this commit is not included in 2.3.1. Could you please recheck it? |
Can you please tell me why "it looks this commit is not included in 2.3.1"? |
Sorry, I was wrong |
This code produce compile-time error with any of lines 1 and 2:
but Optional::map handle Function returning null correctly.
The text was updated successfully, but these errors were encountered: