从嵌套泛型推断泛型类型参数

Infer generic type arguments from nested generics

我想出了一个泛型函数 foo,它链接两个泛型函数,同时捕获一个泛型异常并返回一个泛型。但是我不能调用它。类型和参数在这里可能有点混乱,但在这里使用更冗长的名称并不会减少混乱。

函数 bar 应该提供所有通用类型来推断它们,但它显示编译错误,我也不能显式添加它们(execute<E, Class<D>, D, IOException>(...) 结果 'not applicable for the arguments')。

我已经尝试了几个小时,并且使用 fooBar 编译没有问题。所以我想类型兼容性没有问题,而是我如何在 bar 中调用 foo 或如何推断它们。

public class A {
    // similiar to BiFunction but throws Exception
    static interface Foo<P1, P2, R> { R apply(P1 p1, P2 p2) throws Exception; }
    static class Bar extends RuntimeException { /*...*/ }

    public static <R1, D1, R2, E1 extends Exception> R2 foo(Foo<B, C, R1> arg1, C arg2, BiFunction<R1, Class<D1>, R2> arg3, Class<E1> arg4, Class<D1> arg5) {
        try {
            B b1 = new B();
            R1 r1 = arg1.apply(b1, arg2);
            return arg3.apply(r1, arg5);
        } catch (Exception e1) {
            if (arg4.isInstance(e1)) {
                return null;
            }
            throw new Bar();
        }
    }

    public static D bar() {
        Foo<B, C, E> foo1 = (b1, c1) -> b1.foo(c1); // where B.foo(C) returns E
        BiFunction<E, Class<D>, D> bar1 = (e1, cClass) -> e1.bar(cClass); // where E.bar(Class<D>) returns D
        return foo(foo1, new C(), bar1, new IOException(), new D()); // compile error: cannot infer generic type arguments
    }

    public static D fooBar() {
        Foo<B, C, E> foo1 = (b1, c1) -> b1.foo(c1); // where B.foo(C) returns E
        BiFunction<E, Class<D>, D> bar1 = (e1, cClass) -> e1.bar(cClass); // where E.bar(Class<D>) returns D
        try {
            B b1 = new B();
            E e1 = foo1.apply(b1, new C());
            return bar1.apply(e1, D.class);
        } catch (Exception e1) {
            if (IOException.class.isInstance(e1)) {
                return null;
            }
            throw new Bar();
        }
    }
}

粘贴和格式化花费了无限长的时间,如果我输入有误,我深表歉意。 我如何调用foo以便推断通用类型参数?

foo 的第四个和第五个参数期望 Classes,但你给它一个 IOException 和一个 D.

您可能打算给它 ​​IOException.classD.class:

return foo(foo1, new C(), bar1, IOException.class, D.class);