我有以下的使用Vavr的Java代码片段。类型检查失败,除非我内联一个参数。Javac不能推断类型,除非lambda表达式被内联
为什么下面的代码不能被编译器接受?
import io.vavr.Function1;
import io.vavr.Tuple;
import io.vavr.Tuple2;
import io.vavr.collection.List;
import io.vavr.Option;
import static io.vavr.collection.List.unfoldRight;
class A {}
class B {}
class Main {
Function1<A, Option<Tuple2<B, A>>> f = (a) -> Option.of(Tuple.of(new B(), new A()));
List<B> L0 = unfoldRight(new A(), f); // *
List<B> L1 = unfoldRight(new A(), (a) -> Option.of(Tuple.of(new B(), new A()));
Option<Tuple2<B, A>> g(A a) { return Option.of(Tuple.of(new B(), new A())); }
List<B> L2 = unfoldRight(new A(), (a) -> g(a)); // **
}
// * Compilation fails with: "Incompatible equality constraint: ? extends T and A"
// ** Compilation fails with: "Incompatible equality constraint: ? extends A and A"
下面是来自Vavr库unfoldRight方法签名:
static <T, U> List<U> unfoldRight(T seed, Function<? super T, Option<Tuple2<? extends U, ? extends T>>> f)
,这里是在Github上的文档的链接相同:
哦,对了,这是'''不兼容的不等式约束:(?延伸A,A)'''不'''不兼容的不等式约束:( ?扩展A,?扩展A)'''。非常感谢! – Zaaier