2016-07-30 55 views
1

在這個片段y.run不typecheck。單面變壓器與scalaz流

object Test { 

    type StateStringTask[A] = StateStringT[Task, A] 
    type StateStringT[M[_], A] = StateT[M, String, A] 

    val x: Process[Task, Unit] = ??? 

    val y: Process[StateStringTask, Unit] = ??? 

    x.run // This typechecks 

    y.run // This fails 
} 

編譯器顯示此錯誤:

could not find implicit value for parameter C: scalaz.Catchable[[x]Test.StateStringTask[x]]

我一定要爲StateStringTask創建Catchable實例?我怎麼做?或者在運行Process時有更簡單的方法來處理有狀態的影響?

回答

0

我想這是不理想的,但我通過使StateStringTaskCatchable實例得到它:

implicit val stateStringTaskInstance: Catchable[StateStringTask] = 
    new Catchable[StateStringTask] { 
    // `a.attempt` stackoverflows, don't ask me why :) 
    def attempt[A](a: StateStringTask[A]): StateStringTask[Throwable \/ A] = a >>= (
     x => Catchable[Task].attempt(Applicative[Task].pure(x)).liftM[StateStringT] 
    ) 
    def fail[A](err: Throwable) = Catchable[Task].fail(err).liftM[StateStringT] 
    } 

葫蘆StateTProcessTask的效果。例如:

def received(queue: Queue[Event]): Process[StateStringTask, Event] = { 
    val toStateStringTask = new (Task ~> StateStringTask) { 
     def apply[A](t: Task[A]): StateStringTask[A] = t.liftM[StateStringT] 
    } 
    // queue.dequeue: Process[Task, Event] 
    queue.dequeue.translate(toStateStringTask) 
    }