Why does not sequence work with List of Validations

806 views Asked by At

I think I understand what sequence is. I am wondering why it does not work with List[ValidationNel]. For instance:

The sequence works fine with List[Option]]

scala> val os = List(1.some, 2.some)
os: List[Option[Int]] = List(Some(1), Some(2))

scala> os.sequence
res10: Option[List[Int]] = Some(List(1, 2))

... but does not work with List[ValidationNel]

scala> val vs: List[ValidationNel[String, Int]] = List(Success(1), Success(2))
vs: List[scalaz.ValidationNel[String,Int]] = List(Success(1), Success(2))

scala> vs.sequence
<console>:15: error: could not find implicit value for parameter ev:scalaz.Leibniz.===[scalaz.ValidationNel[String,Int],G[B]]

... however sequenceU does work with List[ValidationNel]

scala> vs.sequenceU
res14: scalaz.Validation[scalaz.NonEmptyList[String],List[Int]] = Success(List(1, 2))

My questions are: Why does not sequence work with List[ValidationNel] ? Why does sequenceU work with it ?

2

There are 2 answers

6
melps On BEST ANSWER

.sequenceU uses the Unapply technique to derive the correct types, where as for .sequence you need to manually provide the types for it.

To make things a bit more annoying, the first type argument of sequence needs a type parameter that takes one type parameter not two like ValidationNel. So you either type lambda it, or do a local type definition.

Try

type X = ValidationNel[String,X]

vs.sequence[X, Int]

or

vs.sequence[({type l[A]=ValidationNel[String,A]})#l,Int]
1
digit plumber On

Not an expert on Scalaz.

To be short, because ValidationNel is not monad, so this conversion is not valid:

List[ValidationNel[SomeType]] => ValidationNel[List[SomeType]]

as the error message shows: implicit value not found, which indicates no such conversion.