我是 Kotlin 的 Arrow 框架的新手,我有几个问题:
让我们假设
fun getUser(id: Int): IO<Option<User>>
fun getCards(user: User): IO<List<Card>>
fun getUserAndCards(id: Int): IO<Option<Pair<User, List<Card>>>> = IO.fx {
when (val user = !userRepository.get(id)) {
is None -> None
is Some -> {
val cards = !cardRepository.get(user.t.id)
Some(Pair(user.t, cards))
}
}
}
如何以“箭头时尚”的方式实现相同的功能?
我设法得到:
fun getUserAndCards(id: Int): IO<Option<Pair<User, List<Card>>>> = IO.fx {
userRepository.get(id).bind().map { user ->
val cards = cardRepository.get(user.id).bind()
Pair(user, cards)
}
}
但我Suspension functions can be called only within coroutine body
在第二个中获得bind()
。
编辑:我看到这个帖子有同样的问题。在提供的答案中,它说问题是未涵盖 left/none 选项。但是它被覆盖了,当申请map
到 a时None
,它预计会获得None
.