简体   繁体   中英

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._ ?

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM