我正在尝试创建一个具有两种通用类型的函数:一种是具体化的,另一种是从其使用的上下文中派生的(因为它是一个扩展函数):
inline fun <reified E, A> Either<Throwable, A>.bypassLeft(transformation: Throwable.() -> A): Either<Throwable, A> =
when (this) {
is Either.Left -> when (value) {
is E -> value.transformation().right()
else -> this
}
else -> this
}
想法是调用仅提及具体类型的函数,例如:
a.bypassLeft<NoResultException> { "" }
其中“a”是 Either<Throwable,String> 类型的对象
但是编译器并没有让我放弃它,并且要求我指定两种泛型类型,而不是从调用函数的对象中派生第二种类型。这似乎是一件很合理的事情,但也许我错了......
这有可能实现吗?如果是这样,我做错了什么?