Scalaz:如何将函数应用于ValidationNel的成功?

时间:2016-08-03 10:01:32

标签: scala scalaz

这是我的代码x :->GetUsersByPhone x: scalaz.ValidationNel[ValidationError, Seq[PhoneNumberWithIdentifier]]

GetusersByPhone只是一个包装Seq[PhoneNumberWithIdentifier]

的案例类

我的问题是我认为:->运算符是正确的,但它引发了这个错误:

[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]            ^

我该如何解决?我是否需要导入scalaz._scalaz.Scalaz._以外的其他内容?

1 个答案:

答案 0 :(得分:1)

简单map应该有效,因为Validation是偏右的:

x.map(GetUsersByPhone)

方法:->来自Bifunctor个实例。

但是,虽然ValidationNel[A, B]只是Validation[NonEmptyList[A], B]的类型别名,并且BifunctorValidation个实例,但编译器仍然无法找到{ {1}} {1}}的实例。

Bifunctor强制转换为相应的ValidationNel也会有效:

ValidationNel