为什么是 !运算符是否与Arrow Monad理解中的.bind相同?

问题描述

我们可以bind()进行“箭”单子组合的一种方法是“大喊大叫”(第三个示例):

/**
 * All possible approaches to running [Kind] in the context of [Fx]
 *
 * ```
 * fx {
 *   val one = just(1).bind() // using bind
 *   val (two) = just(one + 1) // using destructuring
 *   val three = !just(two + 1) // yelling at it
 * }
 * ```
 */

由于Kotlin的!运算符用于取反布尔值,您能解释一下它如何以及为什么在Arrow中以这种方式工作吗?

解决方法

我在Kotlin的文档中找到了有关运算符重载的答案:https://kotlinlang.org/docs/reference/operator-overloading.html

BindSyntax覆盖not运算符:

interface BindSyntax<F> {

  suspend fun <A> Kind<F,A>.bind(): A

  suspend operator fun <A> Kind<F,A>.not(): A =
    bind()
}