给定以下功能:
context(Raise<ApplicationError>)
suspend fun getUser(legalId: UserLegalId): User
context(Raise<ApplicationError>)
suspend fun getDepartment(departmentCode: DepartmentCode): Department
context(Raise<ApplicationError>)
suspend fun save(user: User): User
我想并行调用它们并累积它们的错误:
context(Raise<Nel<ApplicationError>>)
override suspend fun execute(param: AddUserToDepartmentInfo): Department {
val pair: Pair<User, Department> =
parZipOrAccumulate(
{e1, e2 -> e1 + e2},
{ getUser(param.userLegalId) },
{ getDepartment(param.departmentCode) }
) { a, b -> Pair(a, b) }
saveUserDrivenPort.save(pair.first.copy(departmentId = param.departmentCode))
return pair.second
}
但是,parZipOrAccumulate中的getUser()
和getDepartment()
调用无法编译:
No required context receiver found: Cxt { context(arrow.core.raise.Raise<xxx.ApplicationError>) private open suspend fun getUser(...)
1条答案
按热度按时间drkbr07n1#
你能试着省略
{e1, e2 -> e1 + e2}
参数吗?在这种情况下不需要,因为Nel
累加器是由外部Nel<ApplicationError>
推断的。我在本地试过了,它对我很有效。
Raise<ApplicationError>
在parZipOrAccumulate
的lambda中被暴露为ScopedRaiseAccumulate<ApplicationError>
。