Why isn't Validation a Monad?

后端 未结 2 1774
庸人自扰
庸人自扰 2020-12-02 20:48

an example use case:

def div2(i: Int): Validation[String, Int] = 
    if (i%2 == 0) Validation.success(i/2)
    else Validation.failure(\"odd\")

def div4(i:         


        
2条回答
  •  暖寄归人
    2020-12-02 21:24

    As discussed in the Scalaz group, the problem seems to be that ap would accumulate errors whereas (pseudo-)monadic composition would only operate on the value part of Validation.

    Therefore, one cannot be expressed in terms of the other and thus no monad instance exists for Validation.

提交回复
热议问题