【问题标题】:ValidationNel Error after upgrading Scalaz to 7.2将 Scalaz 升级到 7.2 后的 ValidationNel 错误
【发布时间】:2018-01-04 04:04:10
【问题描述】:

在将 Scalaz 版本升级到 7.2 后,我遇到了验证问题。以下代码在之前的 Scalaz 版本中运行。

 def registerOrUpdate(enc: EncAdt, dx: List[DiagnosisAdt], provs: List[Provider], plans: List[InsurancePlan]): ValidationNel[AdtError, String \/ Int] = {
        // First check that admit date is after contract start
        enc.admitDT.fold[ValidationNel[AdtError, String \/ Int]](
          MissingAdmitDate(enc).failureNel
        ) { admitTstamp =>
          val beforeContractDate = fac.dosStart.exists(_ isAfter new DateTime(admitTstamp.getTime))
          if (enc.accountNumber.trim == "") {

            MissingFin(enc).failureNel
          } else {
             ...

升级 Scalaz 版本后,出现以下问题。

fold does not take type parameters

任何解决方案都是可以理解的。

【问题讨论】:

    标签: scala scalaz


    【解决方案1】:

    由于您再次没有在您的问题中提供Minimal, Complete, and Verifiable example,因此很难为您提供适当的帮助。如果我从我的previous answer 复制第一个AdtValidation

    object AdtValidation {
    
      type AdtValidation[A] = ValidationNel[AdtError, A]
    
      implicit class AdtValidationSuccess[A](val value: A) extends AnyVal {
        def successAdt: AdtValidation[A] = Validation.success[NonEmptyList[AdtError], A](value)
      }
    
      implicit class AdtValidationFailure(val value: AdtError) extends AnyVal {
        def failureAdt[A]: AdtValidation[A] = Validation.failureNel[AdtError, A](value)
      }
    }
    

    然后您的代码的以下简化将为我编译(前提是您定义了其他类,例如 EncAdtMissingAdmitDate

    import AdtValidation._
    
    // the original works as well but this seems better to me
    def registerOrUpdate(enc: EncAdt): AdtValidation[String \/ Int] = {
    //def registerOrUpdate(enc: EncAdt): ValidationNel[AdtError, String \/ Int] = {
      // First check that admit date is after contract start
      enc.admitDT.fold[ValidationNel[AdtError, String \/ Int]](
        //  MissingAdmitDate(enc).failureNel
        MissingAdmitDate(enc).failureAdt
      ) { admitTstamp =>
        //        val beforeContractDate = fac.dosStart.exists(_ isAfter new DateTime(admitTstamp.getTime))
        if (enc.accountNumber.trim == "") {
    
          // MissingFin(enc).failureNel
          MissingFin(enc).failureAdt
        } else {
          //...
          -\/(enc.transactionID.toString).successNel  // this works
          -\/(enc.transactionID.toString).successAdt  // this also works
    
        }
      }
    }
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多