Java:部分应用的泛型类-如何消除冗余类型参数? [英] Java: Partially applied generic classes - how to eliminate redundant type parameters?

查看:116
本文介绍了Java:部分应用的泛型类-如何消除冗余类型参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java应用程序中,我创建了返回 Either<A, B> 对象.

In my Java application I created methods that return Either<A, B> objects.

但是我真正使用的类型是Either<String, T>,即String始终是左类型参数,而右参数可以是任何类型.

However the type I really use is Either<String, T>, i.e. String is always the left type parameter, while the right parameter can be any type.

这是我正在使用的 functionaljava Either实现:

Here is the functionaljava Either implementation I am using:

https://github.com/functionaljava/functionaljava/blob/master/core/src/main/java/fj/data/Either.java

此处Either定义为:

public abstract class Either<A, B>

为了使我的代码不那么冗长,我想创建一个通用类LeftAppliedEither<T>,它将代表一个Either,其中的左类型参数设置为String.

To make my code less verbose, I would like to create a generic class LeftAppliedEither<T>, which will represent an Either in which the left type parameter is set to be String.

所以我猜想是这样的:

public abstract class LeftAppliedEither<T> extends Either<String, T> {}

但是这不起作用.

首先,由于我的Either唯一构造函数定义为private,因此我无法对其进行扩展.

First, Either cannot be extended by me since its only constructor is defined as private.

第二,假设我已经解决了第一个问题,只需将Either的代码复制到我的代码中(我们称它为MyEither)并删除私有构造函数(并解决一些较小的编译错误).

Second, let's assume I have solved the first problem by simply copying Either's code into my code (let's call it MyEither) and removing the private constructor (and solving some minor compilation errors).

因此我的代码中包含以下类:

So I have the following class in my code:

package fj.data;

//import ....

public abstract class MyEither<A, B> {
    //  private MyEither() {
    //
    //  }

    //the rest of the code is more or less like in the original Either

}

仍然,我会遇到以下问题:

Still, I would have the following problem:

我无法编写以下代码:

LeftAppliedEither<Integer> hello = LeftAppliedEither.left("hello");

我只能做类似的事情:

MyEither<String,Integer> hello = LeftAppliedEither.left("hello");

好吧,这与我进行此更改的全部原因不符-我不想在我的代码中使用带有两个参数的泛型类型,因为指定左侧的String是多余的.

Well, that defeats the whole reason I was making this change - I wanted not to be required to use in my code the generic type with two parameters, since specifying the left String is redundant.

除了重写整个LeftAppliedEither类之外,还有更好,更优雅的方法来实现这一目标吗?

Are there better and more elegant ways to achieve this, other than rewriting the whole LeftAppliedEither class?

推荐答案

您在这里拥有的是一个静态方法:

What you are having here is a static method:

LeftAppliedEither<Integer> hello = LeftAppliedEither.left("hello");

此静态方法不受继承影响.如您在代码中所见,它带来了自己的泛型.因此继承对您没有帮助:

This static method is not affected by inheritance. As you can see in the code, it brings its own generics. So inheritance does not help you here:

/**
 * Construct a left value of either.
 * @param a The value underlying the either.
 * @return A left value of either.
 */
public static <A, B> Either<A, B> left(final A a) {
    return new Left<A, B>(a);
}

因此,基本上,您需要做的是重构完整的任一类,以用字符串替换每个"A",并删除通用参数中的所有"A",如下例所示:

So basically what you need do is to refactor the complete either-class to replace each "A" by a String and remove all "A" in the generic-parameters as shown in this example:

/**
 * Construct a left value of either.
 * @param a The value underlying the either.
 * @return A left value of either.
 */
public static <B> MyEither<B> left(final String a) {
    return new MyLeft<B>(a);
}

不幸的是,您无能为力(显然,除了注释中提到的每次都写"String".这可能是多余的,但它也可以帮助您清楚地理解代码.所以我下降是有用的)

Unfortunately there is not much more what you can do (except the obvious, just write the "String" each time as mentioned in the comments. It may be redundant, but it also helps you clearly to understand the code. So I fell it is useful)

这篇关于Java:部分应用的泛型类-如何消除冗余类型参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆