Wydaje się, że używasz sprawdzania poprawności dla efektu ubocznego. To nie jest to, o czym jest. Wartości zwracane są używane w programowaniu funkcjonalnym.
Sprawdzanie poprawności w celu zrozumienia nadal trwa, ale powoduje awarię i zwraca błąd.
scala> def g(i: Int): Validation[String, Int] = {
println(i); if(i % 2 == 0) i.success else "odd".fail
}
g: (i: Int)scalaz.Validation[String,Int]
scala> val result = for {
| i <- g(1)
| j <- g(2)
| } yield (i,j)
1
result: scalaz.Validation[String,(Int, Int)] = Failure(odd)
scala> val result = for {
| i <- g(2)
| j <- g(1)
| } yield (i,j)
2
1
result: scalaz.Validation[String,(Int, Int)] = Failure(odd)
scala> val result = for {
| i <- g(2)
| j <- g(2)
| } yield (i,j)
2
2
result: scalaz.Validation[String,(Int, Int)] = Success((2,2))
scala> val result = for {
| i <- g(1)
| j <- g(1)
| } yield (i,j)
1
result: scalaz.Validation[String,(Int, Int)] = Failure(odd)
Cześć, to trochę dziwne, zobaczyć, co chcesz osiągnąć. Jeśli chcesz przeprowadzić walidację łańcuchową, proponuję zajrzeć tutaj (aplikacyjne) w linii 97, co może pomóc: https: //github.com/scalaz/scalaz/blob/master/example/src/main/scala/scalaz/example/ExampleValidation .scala – AndreasScheinert