3

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)
Kevin Meredith
  • 41,036
  • 63
  • 209
  • 384
  • Can you please provide the complete contents of `Validation`. In `map2` you have used type `E` which is not declared in `map2` declaration. And because it is not shown above how `validation` was initialized hence we do not know the type `E` and `A` – Jatin Jan 08 '14 at 05:24
  • Moreover it looks as expected as from `fa` argument, `E` is of type `Nothing` and `A` of type `String`. So it expects `fb` of type `Validation[Nothing, ?]` and you are passing `Validation[String, Nothing]` – Jatin Jan 08 '14 at 06:29
  • @Jatin, thanks for pointing out the wrong types. Using your comment and this `type lambda` post (http://stackoverflow.com/questions/8736164/what-are-type-lambdas-in-scala-and-what-are-their-benefits), I understood that I needed to add a type: `val applicative = ValidationApplicative.validationApplicative[String]`. Note that, before, I did not specify the `[String]`. – Kevin Meredith Jan 11 '14 at 05:06

0 Answers0