我有以下的使用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