我的代码
open class Fail(override val message: String, override val cause: Throwable?) : RuntimeException(message, cause)
data class ValidationFail(override val message: String, override val cause: Throwable?) : Fail(message, cause)
将来会在那里定义更多失败
我有 2 个功能
fun fun1(): Either<out Fail, A>
fun fun2(a: A): Either<out Fail, B>
当我尝试像这样调用它们时,fun1().flatMap{fun2(it)}
我得到了
Type mismatch: inferred type is (A!) -> Either<out Fail, B> but ((A!) -> Nothing)! was expected. Projected type Either<out Fail, A> restricts use of public final fun <U : Any!> flatMap(p0: ((R!) -> Either<L!, out U!>!)!): Either<L!, U!>! defined in io.vavr.control.Either
来自 vavr 的代码
default <U> Either<L, U> flatMap(Function<? super R, ? extends Either<L, ? extends U>> mapper) {
Objects.requireNonNull(mapper, "mapper is null");
if (isRight()) {
return (Either<L, U>) mapper.apply(get());
} else {
return (Either<L, U>) this;
}
}
我猜 o 有这个错误,因为L
在 flatMap 定义中没有? extends L
有什么解决方法吗?