2017-08-31 71 views
2

我有兩個DSL - EmployeeActionContactAction。這裏是我的特質(操作)使用免費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]] 

我用catsCoproductInject這裏是我的計劃,讓經理地址:

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是哪個呼叫失敗。

+0

'EitherT'的編譯錯誤是什麼? –

+0

@ZiyangLiu我在這個問題 – arjunswaj

回答

0

EitherT需要F[Either[A, B]]但您有一個Free[Program, Either[Error, Employee]],這是不兼容的。

的解決方案來創建一個類型別名Free[Program, A]

type MyAlias[A] = Free[Program, A] 

然後讓getEmployee回報MyAlias[Either[Error, Employee]]和同爲getAddress

+0

實際更新錯誤,[這裏是要點]的我有什麼(https://gist.github.com/arjunswaj/4c3c7789ccdd9f832f2cf16690b57cbf)。即使類型別名的理解是不工作的EitherT和自由不能合併。我想知道如何處理這個。 – arjunswaj