本文介绍了将 Scalaz 升级到 7.2 后出现 ValidationNel 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将 Scalaz 版本升级到 7.2 后,我遇到了验证问题.以下代码适用于之前的 Scalaz 版本.

I'm running into validation issues after upgrading the Scalaz version to 7.2. The following code was working in prior Scalaz version.

 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 版本后,正在生成以下问题.

After upgrading Scalaz version following issue is being generated.

fold does not take type parameters

任何解决方案都是可观的.

Any solution is appreciable.

推荐答案

由于您再次没有提供 最小、完整和可验证的示例 在您的问题中,很难正确地帮助您.如果我从我的以前的答案中复制第一个AdtValidation:

Since you again didn't provide a Minimal, Complete, and Verifiable example in your question, it is hard to help you properly. Still if I copy the first AdtValidation from my previous answer:

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)

then the following simplification of your code compiles for me (provided you define other classes such as EncAdt or MissingAdmitDate)

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

    }
  }
}

希望这会有所帮助.

这篇关于将 Scalaz 升级到 7.2 后出现 ValidationNel 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-06 17:02