Flo
Flo

Reputation: 68

Scalaz Validation disregard Apply method

    (List("ha", "heh", "hmm") |@| List("?", "!", ".")) {_ +"doeswork"+ _}

returns correctly

     res0: List[String] = List(hadoeswork?, hadoeswork!, hadoeswork., hehdoeswork?, hehdoeswork!, hehdoeswork., hmmdoeswork?, hmmdoeswork!, hmmdoeswork.)

while

    (Validation.failure[String, String]("fail") |@| Validation.failure[String, String]("fail")) {_ +"doesnotwork"+ _}

returns invariably

    res1: scalaz.Validation[String,String] = Failure(failfail)

whatever function you're passing to "unlift" the values. Any idea why?

Upvotes: 1

Views: 40

Answers (1)

Andrey Tyukin
Andrey Tyukin

Reputation: 44918

It doesn't do this "invariably". If you tried

("fail".success[String] |@| "fail".success[String]) {_ +"doesnotwork"+ _}

it would return Success(faildoesnotworkfail). But since not all values passed to |@| are Success, it doesn't have anything to apply the {_ + "doesnotwork" + _} to, because this operation is for the success case, not for the failure case.

The success case is doing the computations. The failure case only returns the combined error messages. If you try to do computations on Failure, nothing interesting is actually computed, only the summary of errors is returned.

In your particular case, it just happens to be that it seems as if the function is applicable, because both success values and failure messages have the same type. However, in general case, the type of the failure messages doesn't even have to do anything with the type of the function passed instead {_ +"doesnotwork"+ _}. For example, your Success values could be Double, and instead of {_ +"doesnotwork"+ _} you could then use {(x, y) => math.atan2(y, x)}, which wouldn't make any sense for String-typed Failure-messages.

Upvotes: 2

Related Questions