I wanted to implement a similar type called Result
to the Either
type. The main difference is, that the Left side of the Result
type should always be a List of something. What would be the right type definition for this? I tried having something like that:
sealed trait Result[List, +A] {
def map[B](f: A => B): Result[List, B] = this match {
case Failure(err) => Failure(err)
case Success(value) => Success(f(value))
}
def apply[B](f: Result[List, A => B]): Result[List, B] = (f, this) match {
case (Failure(fE), Failure(aE)) => Failure(fE ::: aE)
case ...
}
}
final case class Failure[+E](errors: List[E]) extends Result[List[E], Nothing]
final case class Success[+A](value: A) extends Result[Nothing, A]
But this runs in an error in the map function saying Failure[Any] does not equal Result[List, B]
and Success[B] does not equal Result[List, B]
. Is the type definition Result[List, +A]
already wrong, should I use a higher-kinded type like List[_]
instead?