组成具有不同异常类型的两个函数(java泛型)

马苏夫:

假设我要实现一个java.util.function.Function<T,R>可以抛出某些异常类型的版本E我所做的是创建一个功能接口,ThrowingFunction如下所示:

@FunctionalInterface
interface ThrowingFunction<T, R, E extends Exception>
{
    R apply(T t) throws E;
}

现在就像在中一样java.util.function.Function<T,R>,我需要实现默认方法,compose并且我需要组成两个不同的ThrowingFunction实例,并且可能,它们可以具有不同的抛出异常类型所以这是我的尝试:

default <V, E1 extends Exception, E2 super E1 & E> ThrowingFunction<V, R, E2> compose(ThrowingFunction<? super V, ? extends T, ? extends E1> before)
{
    return v -> apply(before.apply(v));
}

当然,在这里,我得到了一个编译器错误,说:

令牌“ super”上的语法错误,预期扩展

恰好在通用参数的声明中E2 super E1 & E

那么,该问题的可能解决方案是什么,我是否只需编写两个ThrowingFunction具有相同异常类型的不同实例E而这不是最好的)?

rgettman:

您不能default在此处使用方法;它必须是static接口上的方法。当您尝试使用default方法时,类型E已经被捕获,并且Java编译器不允许您将类型声明为E22类E以下的超类E1,这是编译器错误指示的。

更改为static方法时,可以根据需要自由声明异常类型参数。您可以声明超类型异常类型S,然后声明子类型异常类型E1E2然后其余的内容与Function的默认compose方法类似

此处的类型参数与Function.compose使用的内容匹配,并添加了与异常相关的类型参数。

@FunctionalInterface
interface ThrowingFunction<T, R, E extends Exception>
{
    R apply(T t) throws E;

    /**
     * Composes f (after) and g (before) to produce the composed
     * function f o g(v), which executes f(g(v)).
     * @param f The function that will take g's output <T> as input to return the overall result <R>.
     * @param g The function that will take the overall input <V> and return f's input <T>.
     * @param <V> The input result type of the entire composed function (and of function g).
     * @param <T> The result type of g, used as input to f.
     * @param <R> The end result type of the entire composed function (and of function f).
     * @param <S> If any of f or g throws an exception, it is captured by the supertype exception class S.
     * @param <E1> The exception type thrown by f.
     * @param <E2> The exception type thrown by g.
     * @return A ThrowingFunction that whose input <V> is applied to g, whose result is passed to
     *     f to generate the overall result <R>.
     */
    static <V, T, R, S extends Exception, E1 extends S, E2 extends S> ThrowingFunction<V, R, S>
        compose(ThrowingFunction<? super T, ? extends R, ? extends E1> f,
                ThrowingFunction<? super V, ? extends T, ? extends E2> g)
    {
        return v -> f.apply(g.apply(v));
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Java中一个接一个地迭代具有不同类型的两个泛型列表?

Scala-为什么使用具有两个泛型参数的超类型会导致Scala类型检查器对子类型进行不同的处理?

Java泛型:使用一个函数签名干净的方法来处理两个泛型类型

Kotlin 中具有两个类型参数的泛型类

Swift要求两个泛型具有相同的类型

如何检查两个结构在Swift中是否具有相同的泛型参数类型?

具有两个参数的泛型类

继承两个泛型类型

具有两个泛型参数的C#中的泛型方法

如何制作一个用两个不同的泛型类型扩展Iterable的Java接口?

Java泛型与两个泛型超类型的混淆

C#如何对具有泛型类型的两个类使用一种方法?

具有泛型参数类型的函数

c可以声明两个具有相同名称,返回类型但参数不同的函数

Haskell中两个相似的函数如何具有不同的多态类型?

创建两个具有相同名称但参数类型不同的R函数

Kotlin中具有不同类型参数数量的两个函数

具有两个不同函数的参数的调用函数

两个具有不同数据类型的MapRoute

Mockito模拟when()用于不同类型的两个泛型方法

java中的方法重写中,两个方法可以具有不同的返回类型吗?

为什么两个具有不同类型var的多态高阶函数在类型上等效?

在 C# 中,如何使用具有两种不同类型的泛型?

两种不同的泛型类型具有相同的擦除?

有没有办法在方法声明中定义两个泛型类型

是否可以使一个函数接受具有相同定义的两个不同的求和类型?

C语言:从两个具有不同返回值的相似函数中创建一个泛型函数

java中的泛型类型:如何定义一个返回多个不同类型的函数

在两个不同函数中具有两个相似变量的指针