Looking at the Validation
trait from Functional Programming in Scala:
sealed trait Validation[+E, +A]
case class Failure[E](head: E, tail: Vector[E]) extends Validation[E, Nothing]
case class Success[A](a: A) extends Validation[Nothing, A]
My incomplete understanding is that this trait uses types E and children
and A and children
- covariant.
I can instantiate Success
via:
val s = Success("winner")
But, how can I instantiate Failure
? I tried:
val f = Failure[String]("lose", Vector("lost"))
However it failed when I tried to input f
as the fb
argument to map2
:
override def map2[A,B,C](fa: Validation[E,A], fb: Validation[E,B])(f: (A, B) => C)
scala> validation.map2(s, failure)( (a: String, b: String) => a + b)
<console>:17: error: type mismatch;
found : DataType.DataType.Failure[String]
required: DataType.DataType.Validation[Nothing,?]
validation.map2(s, failure)( (a: String, b: String) => a + b)