我有两个DSL - EmployeeAction
和ContactAction
。这里是我的特质(操作)使用免费Monad与任一
完整的要点是:link
sealed trait EmployeeAction[R]
case class GetEmployee(id: Long) extends EmployeeAction[Either[Error, Employee]]
sealed trait ContactAction[R]
case class GetAddress(empId: Long) extends ContactAction[Either[Error, Address]]
我用cats
“Coproduct
和Inject
这里是我的计划,让经理地址:
type Program = Coproduct[EmployeeAction, ContactAction, A]
def findManagerAddress(employeeId: Long)
(implicit EA: EmployeeActions[Program],
CA: ContactActions[Program]): Free[Program, Address] = {
import EA._, CA._
for {
employee <- getEmployee(employeeId)
managerAddress <- getAddress(employee.managerId)
} yield managerAddress
}
以上没有按不会编译,因为getEmployee
返回Either[Error, Employee]
。我如何处理Free
以便理解?
我试过用下面的EitherT
monad变换器,它在IntelliJ中没有显示错误,但是在构建时失败。
for {
employee <- EitherT(getEmployee(employeeId))
managerAddress <- EitherT(getAddress(employee.managerId))
} yield managerAddress
下面是错误:
[scalac-2.11] /local/home/arjun/code/Free/src/FreeScalaScripts/src/free/program/Program.scala:71: error: no type parameters for method apply: (value: F[Either[A,B]])cats.data.EitherT[F,A,B] in object EitherT exist so that it can be applied to arguments (cats.free.Free[free.Program,Either[free.Error,free.Employee]])
[scalac-2.11] --- because ---
[scalac-2.11] argument expression's type is not compatible with formal parameter type;
[scalac-2.11] found : cats.free.Free[free.Program,Either[free.Error,free.Employee]]
[scalac-2.11] required: ?F[Either[?A,?B]]
[scalac-2.11] employee <- EitherT(getEmployee(employeeId))
[scalac-2.11] ^
如何应对无论是在理解和如何传播错误给调用者?我想知道所有的员工ID是哪个呼叫失败。
'EitherT'的编译错误是什么? –
@ZiyangLiu我在这个问题 – arjunswaj