Chobeat Chobeat - 3 months ago 15
Scala Question

Scalaz: how to apply a function to the success of a ValidationNel?

this is my code

x :->GetUsersByPhone
where
x: scalaz.ValidationNel[ValidationError, Seq[PhoneNumberWithIdentifier]]
.

GetusersByPhone
is simply a case class that wraps the
Seq[PhoneNumberWithIdentifier]


My problem is with the
:->
operator that I think is correct but it raises this error:

[error] /home/simone/radicalbit/mpay-user/play/src/main/scala/com/next/mpay/users/validators/users/GetUsersByPhoneValidation.scala:31: value :-> is not a member of scalaz.ValidationNel[com.next.mpay.users.validators.ValidationError,Seq[com.next.mpay.users.persistence.PhoneNumberWithIdentifier]]
[error] possible cause: maybe a semicolon is missing before `value :->'?
[error] .:->(GetUsersByPhone)
[error] ^


How can I fix that? Do I need to import something else besides
scalaz._
and
scalaz.Scalaz._
?

Answer

Simple map should work, because Validation is right-biased:

x.map(GetUsersByPhone)

Method :-> comes from a Bifunctor instance.

But although ValidationNel[A, B] is just a type alias for Validation[NonEmptyList[A], B], and there is a Bifunctor instance for Validation, the compiler still can't find a Bifunctor instance for ValidationNel.

Coercing ValidationNel to the respective Validation would also work though:

(x: Validation[NonEmptyList[ValidationError], Seq[PhoneNumberWithIdentifier]])
  .:->(GetUsersByPhone)
Comments