简体   繁体   English

验证:隐式scalaz.Bind未找到

[英]Validation: implicit scalaz.Bind not found

I have this implementation of scalaz Validation, it seems like that implicitly scalaz.Bind not in the scope, so for expression is not working. 我有scalaz验证的这个实现,似乎隐含scalaz.Bind不在范围内,所以表达式不起作用。 Here is the code: 这是代码:

import scalaz._
import Scalaz._

case class User(name: String, knowScala: Boolean, age: Int)

object PublicValidationSpec extends BasicValidation {
  def validate(user: User): Validation[String, String] = {
    for {
      b <- checkAge(user)
      c <- checkName(user)
    } yield s"Congrats, ${c.name}"
  }
}

trait BasicValidation {
  def checkName(user: User): Validation[String, User] = {
    if(user.name == "") "must have a name".fail else user.success
  }

  def checkAge(user: User): Validation[String, User] = {
    if(user.age < 3) "must be a valid age".fail else user.success
  }
}

the exception is 例外是

Error:(14, 25) Implicit not found: scalaz.Unapply[scalaz.Bind, 
scalaz.Validation[String,scalaz.validation.User]]. Unable to unapply type 
`scalaz.Validation[String,scalaz.validation.User]` into a type constructor of 
kind `M[_]` that is classified by the type class `scalaz.Bind`. Check that the 
type class is defined by compiling `implicitly[scalaz.Bind[type constructor]]` and 
review the implicits in object Unapply, which only cover common type 'shapes.'
           b <- checkAge(user)

Did i miss some implicit imports here ?
                        ^

Validation does not have a Bind defined for it. 验证没有为其定义Bind。

In Scalaz 7.1.0-M5 (M6 too) Validation.flatMap is deprecated and in an attempt at subverting the warning, it looks like the precedence of flatMap is losing to scalaz.syntax.bind._ , which is part of the Scalaz._ import. 在Scalaz 7.1.0-M5(也是M6)中,不推荐使用Validation.flatMap ,并试图破坏警告,看起来flatMap的优先级正在丢失到scalaz.syntax.bind._ ,这是Scalaz._一部分Scalaz._导入。 See this commit https://github.com/scalaz/scalaz/commit/2727b2451eba2aa159f3fbb62bf92790ac99dc7a . 请参阅此提交https://github.com/scalaz/scalaz/commit/2727b2451eba2aa159f3fbb62bf92790ac99dc7a Try adding import scalaz.Validation.FlatMap._ or importing only the things you need, eg 尝试添加import scalaz.Validation.FlatMap._或仅导入您需要的内容,例如

import scalaz.Validation
import scalaz.syntax.validation._

I would recommend using something besides Validation though, as this will probably only cause you more problems in the future. 我建议使用除Validation之外的东西,因为这可能只会在将来引起更多问题。 See scalaz.\\/ below. scalaz.\\/下面。

This does compile with scalaz 7.0.5. 这可以使用scalaz 7.0.5进行编译。 Validation.flatMap is defined in 7.0.6 so it should compile with that version too. Validation.flatMap在7.0.6中定义,因此它也应该使用该版本进行编译。 I would not use this functionality ( Validation in for comprehensions) in new code though. 我不会在新代码中使用此功能( Validationfor Validation )。

The scalaz powers that be have gona back and forth on deprecating Validation.flatMap for some time now. scalaz权力在弃用Validation.flatMap已经有一段时间了。 flatMap is what allows it to work in for comprehensions. flatMap是允许它for理解的东西。 flatMap is deprecated in the working branch though . flatMap 工作分支虽然弃用 There is a long background on this. 这有很长的背景。 See https://groups.google.com/forum/#!topic/scalaz/Wnkdyhebo2w . 请参阅https://groups.google.com/forum/#!topic/scalaz/Wnkdyhebo2w

TLDR - Validation is not a monad. TLDR - 验证不是monad。 Any possible implementation of Validation.flatMap will not match behavior of the Apply defined for Validation . Validation.flatMap任何可能实现都不会与为Validation定义的Apply行为相匹配。

Use scalaz.\\/ (aka disjunction) if you want to use something in for comprehensions. 如果你想使用某些东西for理解,请使用scalaz.\\/ (又称scalaz.\\/取)。 If you need to accumulate errors, the reason to use Validation over \\/ , convert to Validation and then back to \\/ . 如果您需要累积错误,使用Validation的原因是\\/ ,转换为Validation然后返回\\/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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