2

I'm trying to use Scalaz (scala 2.10, version 7.1 of ScalaZ) for validations. I have a case class with 13 fields, so I end up with 13 validations. I'm using the combinator to combine all of the validations and build the class if all are successful. If there are just 12 combinators, everything is fine. When I add the 13th, I get a message saying that "value |@| is not a member of scalaz.syntax.ApplicativeBuilder".

To reproduce, I fired up the REPL and tried to combine 12 items:

scala> (1.some |@| 2.some |@| 3.some |@| 4.some |@| 5.some |@| 6.some |@| 7.some |@|
        8.some |@| 9.some |@| 10.some |@| 11.some |@| 12.some ) 
        {_ + _ + _ + _ + _ + _ + _ + _ + _ +_ +_ +_ }
res11: Option[Int] = Some(78)

works just fine. I then tried to combine 13 items:

scala> (1.some |@| 2.some |@| 3.some |@| 4.some |@| 5.some |@| 6.some |@| 7.some |@|
        8.some |@| 9.some |@| 10.some |@| 11.some |@| 12.some |@| 13.some) 
        {_ + _ + _ + _ + _ + _ + _ + _ + _ +_ +_ +_ + _}
<console>:14: error: value |@| is not a member of
scalaz.syntax.ApplicativeBuilder[Option,Int,Int]#ApplicativeBuilder3[Int]#
ApplicativeBuilder4[Int]#ApplicativeBuilder5[Int]#ApplicativeBuilder6[Int]#
ApplicativeBuilder7[Int]#ApplicativeBuilder8[Int]#ApplicativeBuilder9[Int]#
ApplicativeBuilder10[Int]#ApplicativeBuilder11[Int]#ApplicativeBuilder12[Int]
(1.some |@| 2.some |@| 3.some |@| 4.some |@| 5.some |@| 6.some |@| 7.some |@| 8.some |@| 9.some |@| 10.some |@| 11.some |@| 12.some |@| 13.some) {_ + _ + _ + _ + _ + _ + _ + _ + _ +_ +_ +_ + _}

Is there another way to combine all of the validations?

4

1 回答 1

4

为了完整起见:是的,您可以应用应用构建器语法的项目数量是有限的,是的,它是十二个。不过,一般情况下,您可以重写以下内容:

(a |@| b |@| c)(Function.uncurried(foo))

作为:

c <*> (b <*> a.map(foo))

此语法适用于超过 12 个元素。有关更多详细信息,请参见我的答案

于 2014-08-17T18:32:07.227 回答