解构而不是 .bind() 在 Arrow Monad 理解中不起作用
Destructuring instead of .bind() doesn't work in an Arrow Monad comprehension
根据 Arrow 的 Javadoc,有三种绑定 monad 的方法:
/**
* 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
* }
* ```
*/
第一个和最后一个工作正常但是由于某些原因解构没有,为什么?
在下一个示例中,您可以看到我正在使用解构,val (j) = helloJoey().k(),) but that value is interpreted as a
Mono` 而不是字符串。
class HelloServiceImpl : HelloService {
private val logger = LoggerFactory.getLogger(javaClass)
override fun helloEverybody(): Mono<out String> {
return MonoK.monad().fx.monad {
val (j) = helloJoey().k()
val a = !Mono.zip(helloJohn(), helloMary()).map { "${it.t1} and ${it.t2}" }.k()
"$j and $a"
}.fix().mono
}
override fun helloJoey(): Mono<String> {
return Mono.defer {
logger.info("helloJoey()")
sleep(2000)
logger.info("helloJoey() - ready")
Mono.just("hello Joey")
}.subscribeOn(Schedulers.elastic())
}
override fun helloJohn(): Mono<String> {
return Mono.defer {
logger.info("helloJohn()")
sleep(5000)
logger.info("helloJohn() - ready")
Mono.just("hello John")
}.subscribeOn(Schedulers.elastic())
}
override fun helloMary(): Mono<String> {
return Mono.defer {
logger.info("helloMary()")
sleep(5000)
logger.info("helloMary() - ready")
Mono.just("hello Mary")
}.subscribeOn(Schedulers.elastic())
}
}
fun main() {
val countDownLatch = CountDownLatch(1)
HelloServiceImpl().helloEverybody().subscribe {
println(it)
countDownLatch.countDown()
}
countDownLatch.await()
}
这是一个已知问题,也是我们放弃这种方法的原因。它们被标记为已弃用 here.
发生的情况是,MonoK
是一个数据 class,其解构运算符已定义为返回包装的 Mono
。当在 fx
块内使用时,此解构优先于 BindSyntax
上定义的解构。检查并查看提示您的预期类型是否有效,否则请改用 invoke
或 bind
。
根据 Arrow 的 Javadoc,有三种绑定 monad 的方法:
/**
* 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
* }
* ```
*/
第一个和最后一个工作正常但是由于某些原因解构没有,为什么?
在下一个示例中,您可以看到我正在使用解构,val (j) = helloJoey().k(),) but that value is interpreted as a
Mono` 而不是字符串。
class HelloServiceImpl : HelloService {
private val logger = LoggerFactory.getLogger(javaClass)
override fun helloEverybody(): Mono<out String> {
return MonoK.monad().fx.monad {
val (j) = helloJoey().k()
val a = !Mono.zip(helloJohn(), helloMary()).map { "${it.t1} and ${it.t2}" }.k()
"$j and $a"
}.fix().mono
}
override fun helloJoey(): Mono<String> {
return Mono.defer {
logger.info("helloJoey()")
sleep(2000)
logger.info("helloJoey() - ready")
Mono.just("hello Joey")
}.subscribeOn(Schedulers.elastic())
}
override fun helloJohn(): Mono<String> {
return Mono.defer {
logger.info("helloJohn()")
sleep(5000)
logger.info("helloJohn() - ready")
Mono.just("hello John")
}.subscribeOn(Schedulers.elastic())
}
override fun helloMary(): Mono<String> {
return Mono.defer {
logger.info("helloMary()")
sleep(5000)
logger.info("helloMary() - ready")
Mono.just("hello Mary")
}.subscribeOn(Schedulers.elastic())
}
}
fun main() {
val countDownLatch = CountDownLatch(1)
HelloServiceImpl().helloEverybody().subscribe {
println(it)
countDownLatch.countDown()
}
countDownLatch.await()
}
这是一个已知问题,也是我们放弃这种方法的原因。它们被标记为已弃用 here.
发生的情况是,MonoK
是一个数据 class,其解构运算符已定义为返回包装的 Mono
。当在 fx
块内使用时,此解构优先于 BindSyntax
上定义的解构。检查并查看提示您的预期类型是否有效,否则请改用 invoke
或 bind
。