在我的 Java 应用程序中,我创建了返回 Either<A, B>
的方法。对象。
但是我真正使用的类型是 Either<String, T>
,即String
始终是左侧类型参数,而右侧参数可以是任何类型。
这是functionaljava Either
我正在使用的实现:
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。
所以我想这样做的方法是:
public abstract class LeftAppliedEither<T> extends Either<String, T> {}
但是这不起作用。
首先,Either
我无法扩展,因为它唯一的构造函数定义为 private
.
其次,假设我通过简单地复制 Either
解决了第一个问题的代码到我的代码中(我们称之为 MyEither
)并删除私有(private)构造函数(并解决一些小的编译错误)。
所以我的代码中有以下类:
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
}
但是,我仍然会遇到以下问题:
我无法编写以下代码:
LeftAppliedEither<Integer> hello = LeftAppliedEither.left("hello");
我只能做这样的事情:
MyEither<String,Integer> hello = LeftAppliedEither.left("hello");
好吧,这违背了我进行此更改的全部原因 - 我不想被要求在我的代码中使用带有两个参数的泛型类型,因为指定了左侧 String
是多余的。
除了重写整个LeftAppliedEither
之外,还有更好、更优雅的方法来实现这一点吗?类?
最佳答案
这里有一个静态方法:
LeftAppliedEither<Integer> hello = LeftAppliedEither.left("hello");
该静态方法不受继承影响。正如您在代码中看到的,它带来了自己的泛型。所以继承在这里对你没有帮助:
/**
* 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”,如本示例所示:
/**
* 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);
}
不幸的是,你没有什么可以做的(除了明显的,每次只写“字符串”,如注释中提到的。它可能是多余的,但它也可以帮助你清楚地理解代码。所以我感觉很有用)
关于Java:部分应用泛型类 - 如何消除冗余类型参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35974005/