简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM