Arrow 1.1.2中option计算API已废弃,求正确替代方案
Arrow 1.1.2中Option Effect废弃警告的替代方案
你遇到的invoke(crossinline suspend OptionEffect<*>.() -> A?): Option废弃警告,是因为旧的option monad comprehension构建器被弃用,官方推荐使用Effect Runtime来替代。
修改后的代码示例
import arrow.core.Some import arrow.core.none import arrow.core.Option import arrow.core.computations.effect suspend fun value(): Option<Int> = //sampleStart effect { val x = none<Int>().bind() val y = Some(1 + x).bind() val z = Some(1 + y).bind() x + y + z }.toOption() //sampleEnd suspend fun main() { println(value()) }
关键修改点
- 替换导入:移除
import arrow.core.computations.option,新增import arrow.core.computations.effect - 替换构建器:将
option { ... }改为effect { ... }.toOption() - 内部逻辑无需改动:原有的
bind()调用可以直接在effect块中使用,Effect Runtime原生支持Option类型的绑定操作
这样修改后就能消除废弃警告,同时适配Arrow 1.1.2的新API标准。
内容的提问来源于stack exchange,提问作者Vencat
相关产品推荐
相关产品推荐

