编译器在 Left(e) 处抱怨:Left(List[ServiceError, Nothing]) 类型的表达式不符合预期的类型 Either[E , R]
sealed trait ServiceResult[+E <: List[ServiceError], +R ] {
def toEither: Either[E , R] = this match {
case Success(a) => Right(a)
case Failure(e) => **Left(e)**
}
}
final case class Success[+R](a: R) extends ServiceResult[Nothing, R] {}
final case class Failure[+T <: ServiceError](e: List[T]) extends ServiceResult[List[T], Nothing]{}
我的要求解释如下,
所以...我有一个特质ServiceError
。后端的每个服务都有自己的错误,这些错误扩展了这个特性。例如,当我从休息层发出请求时,
val r = subnetService ? GetByIdWithInfo( SubnetId( id ) )
val r2 = r.mapTo[ ServiceResult [ SubnetServiceError, SubnetWithInfoDTO ] ] )
我想要一个像 Either[A,B] 这样的类型,但有一些额外的约束。如果服务器上出现错误(或错误),请返回List[ServiceError]
或返回一些result
。