在某些情况下,使用显式类型的泛型方法引用映射 Optional 似乎会导致类型推断失败。在以下示例中,第一行编译,而在给出 List::of
时-method 显式类型 List::<String>of
它不编译:
Optional.of("one,two").map(List::of).get().get(0).split(",");
// No problem, type inference works fine
Optional.of("one,two").map(List::<String>of).get().get(0).split(",");
// Compiler error, because the mapped Optional becomes typed with a raw List parameter (Optional<List>)
在上面的例子中,List::of 的类型提示是不必要的,但在很多情况下是需要的,并且为了帮助编译器必须对映射进行类型提示,从而导致更加冗长
Optional.of("one,two").<List<String>>map(List::of).get().get(0).split(",");
一种情况是如果我们有一些 Either 类型,比如
static class Either<L, R> {
L left;
R right;
private Either(final L left, final R right) {
this.left = left;
this.right = right;
}
static <L, R> Either<L, R> left(final L l) {
return new Either<>(l, null);
}
static <L, R> Either<L, R> right(final R r) {
return new Either<>(null, r);
}
}
然后
final String right =
Optional.of("one,two")
.map(Either::<String, String>left)
.orElse(Either.right("three,four"))
.right;
编译失败,必须编译
final String right =
Optional.of("one,two")
.<Either<String, String>>map(Either::left)
.orElse(Either.right("three,four"))
.right;
为什么会这样,是否有任何改变的计划?
更新:
我正在使用 OpenJDK 15,但这似乎是 Intellij 中的一个问题 - 从命令行运行 javac 所有这些都可以编译,但 Intellij 错误地将上面的一些行突出显示为编译错误。我将关闭并报告错误。
最佳答案
这原来是 Intellij 中的一个错误。
关于java - 方法引用 lambda 中的显式类型提示导致原始类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64339622/