Compiler complains at Left(e): Expression of type Left(List[ServiceError, Nothing]) doesn't conform to expected type 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]{}
My requirement is explained below,
So... I have a trait ServiceError
. Each Service on backend has their own errors which extends this trait. When I'm doing request for example from rest layer,
val r = subnetService ? GetByIdWithInfo( SubnetId( id ) )
val r2 = r.mapTo[ ServiceResult [ SubnetServiceError, SubnetWithInfoDTO ] ] )
I want to have a type like Either[A,B] but with some additional constraints. In case of error ( or errors ) on server - return List[ServiceError]
or return some result
.