1

I have copied an example from http4s:

// Print received Text frames, and, on completion, notify the console
  val sink: Sink[Task, WebSocketFrame] = Process.constant {
    case Text(t) => Task.delay(println(t))
    case f       => Task.delay(println(s"Unknown type: $f"))
  }.onComplete(Process.eval[Task, Nothing](Task{println("Terminated!")}).drain)

This yields a compilation error: "Expression of type Unit doesn't conform to expected type _A"

I just want to printline "Terminated!" when the sink gets terminated

4

1 回答 1

1

It should work if you remove the types from the Process.eval call:

... .onComplete(Process.eval(Task{println("Terminated!")}).drain)

The problem here is that your Process.eval call constructs a Process[Task, Unit] and not a Process[Task, Nothing] as you ask for by giving the types explicity. Calling drain converts the Process[Task, Unit] to a Process[Task, Nothing] afterwards.

Update: Here is an example of a sink that performs some side effect on completion:

scala> val sink = io.stdOutLines.onComplete(Process.eval_(Task(println("END"))))
sink: scalaz.stream.Process[[x]scalaz.concurrent.Task[x],String => scalaz.concurrent.Task[Unit]] = Append(Emit(Vector(<function1>)),Vector(<function1>, <function1>))

scala> Process("a", "b", "c").toSource.to(sink).run.run
a
b
c
END
于 2015-01-14T05:48:05.047 回答