0

コンパイラは Left(e) で不平を言います: 型 Left(List[ServiceError, Nothing]) の式は、期待される型のどちらか [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] のような型を持ちたいのですが、いくつかの追加の制約があります。サーバーでエラー (またはエラー) が発生した場合 - returnList[ServiceError]または return some 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 に答える