在 Scala 中使用来自 Cats 库的验证

Using Validated from the Cats library in Scala

无法理解 Semigroupal.productSemigroupal.tuple2 之间的实际区别。这是一个简短的例子:

import cats.Semigroupal
import cats.data.Validated
import cats.data.Validated.Invalid
import cats.instances.list._ // for Monoid

  type AllErrorsOr[A] = Validated[List[String], A]
  def bothInvalid = {
    Semigroupal[AllErrorsOr].product(
      Validated.invalid(List("Error 1")),
      Validated.invalid(List("Error 2"))
    )
  }

  def bothInvalidTuple = {
    Semigroupal.tuple2(
      Validated.invalid(List("Error 1")),
      Validated.invalid(List("Error 2"))
    )
  }

  def bothValid = {
    Semigroupal[AllErrorsOr].product(
      Validated.valid(10),
      Validated.valid(20)
    )
  }

  def bothValidTuple = {
    Semigroupal.tuple2(
      Validated.valid(10),
      Validated.valid(20)
    )
  }

无效值 bothInvalidbothInvalidTuple 给出相同的结果。对于有效值,仅编译第一个。我得到的错误:

Error:(40, 23) could not find implicit value for parameter semigroupal: cats.Semigroupal[[+A]cats.data.Validated[Nothing,A]] Semigroupal.tuple2(

似乎(如果我没记错的话)Scala 试图找到 Monoid 来组合 Nothing,而不是 List[String]。如何让它与 tuple2 一起工作?

只是一些泛型没有被推断出来。尝试明确指定它们

  type AllErrorsOr[A] = Validated[List[String], A]

  def bothInvalid: AllErrorsOr[(Int, Int)] = {
    Semigroupal[AllErrorsOr].product[Int, Int](
      Validated.invalid(List("Error 1")),
      Validated.invalid(List("Error 2"))
    )
  }

  def bothInvalidTuple: AllErrorsOr[(Int, Int)] = {
    Semigroupal.tuple2[AllErrorsOr, Int, Int](
      Validated.invalid(List("Error 1")),
      Validated.invalid(List("Error 2"))
    )
  }

  def bothValid: AllErrorsOr[(Int, Int)] = {
    Semigroupal[AllErrorsOr].product[Int, Int](
      Validated.valid(10),
      Validated.valid(20)
    )
  }

  def bothValidTuple: AllErrorsOr[(Int, Int)] = {
    Semigroupal.tuple2[AllErrorsOr, Int, Int](
      Validated.valid(10),
      Validated.valid(20)
    )
  }