I'm trying to use Scalaz 7 Validation in my app. However, I'm having an issue getting the |@|
applicative functor to coalesce my failures. Here's the code I have:
type ValidationResult = ValidationNel[String, Unit]
def validate[A: ClassTag](instance: A, fieldNames: Option[Seq[String]] = None): ValidationResult = {
val fields = classTag[A].runtimeClass.getDeclaredFields
val fieldSubset = fieldNames match {
case Some(names) => fields.filter { field => names.contains(field.getName) }
case None => fields
}
fieldSubset.map {
field => field.getAnnotations.toSeq.map {
field.setAccessible(true)
val (name, value) = (field.getName, field.get(instance))
field.setAccessible(false)
annotation => annotation match {
case min: Min => minValidate(name, value, min.value())
case size: Size => sizeValidate(name, value, size.min(), size.max())
}
}
}.flatten[ValidationResult].foldLeft(().successNel[String])(_ |@| _)
}
The minValidate
and sizeValidate
functions just return ValidationResults
.
The problem is, this code won't compile. The error message is:
Type mismatch, expected F0.type#M[NotInferedB], actual: ValidationResult
I have no idea what that means... do I need to give Scala more type info?
What I'm trying to accomplish is, if all fields are successNel
s, then return that, otherwise, return a combination of all the failureNel
s.
Has |@|
changed since previous version of Scalaz? Because even if I do something like:
().successNel |@| ().successNel
I get the same error.
Update
I started poking around the Scalaz source and I found the +++
which seems to do what I want.
What's the difference between +++
and |@|
?