【问题标题】:Why is ! operator the same as .bind() in Arrow Monad comprehension?为什么是 !运算符与 Arrow Monad 理解中的 .bind() 相同?
【发布时间】:2021-02-20 13:30:15
【问题描述】:

我们可以在 Arrow monad 理解中 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 arrow-kt


    【解决方案1】:

    我在 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()
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-01
      • 2021-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-07
      • 1970-01-01
      相关资源
      最近更新 更多