Search code examples
scalavalidationscalazcombinators

Is there maximum number of Validations that can be combined with combinator?


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?


Solution

  • For the sake of completeness: yes, there is a limit on the number of items you can apply the applicative builder syntax to, and yes, it's twelve. In general, though, you can rewrite the following:

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

    As:

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

    This syntax will work for more than twelve elements. See my answer here for more detail.