0

编译器在 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

4

2 回答 2

0

以下内容对您有用吗?

sealed trait ServiceResult[+E <: ServiceError, +R] {
  def toEither: Either[List[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[T, Nothing] {}
于 2015-02-23T22:22:13.910 回答
0

我想你想要的只是

trait ServiceError

trait ServiceResult

type ServiceEither = Either[ List[ ServiceError ], ServiceResult ]

如果这不符合您的要求,请在评论中说明。

于 2015-02-20T12:21:09.660 回答