我了解 free monad,但如果其中一个类采用,例如fs2.Stream
, ,我无法弄清楚如何正确编写代码。
示例代码:
import cats.effect.{Async, IO, IOApp}
import cats.free.Free
import cats.syntax.all._
import cats.{Monad, ~>}
import dev.free.A._
object Example extends IOApp.Simple {
override def run: IO[Unit] = {
val stream =
fs2.Stream.emit[IO, String]("example").through(fs2.text.utf8.encode)
write("/a/b/c", stream).foldMap(compiler)
}
private def compiler: ActionA ~> IO = new (ActionA ~> IO) {
override def apply[A](fa: ActionA[A]): IO[A] = {
fa match {
case Write(path, data) => data.through(fs2.text.utf8.decode).compile.string.flatMap { s =>
IO.println(s"$path; $s")
}
}
}
}
}
object A {
sealed trait ActionA[A]
case class Write(path: String, data: fs2.Stream[_, Byte]) extends ActionA[Unit]
type Action[A] = Free[ActionA, A]
def write(path: String, data: fs2.Stream[_, Byte]): Action[Unit] = Free.liftF[ActionA, Unit](Write(path, data))
}
编译错误:
Example.scala:35:44
_$2 takes no type parameters, expected: 1
def write(path: String, data: fs2.Stream[_, Byte]): Action[Unit] = Free.liftF[ActionA, Unit](Write(path, data))
fs2.Stream
所有问题都是由于没有指定in的效果类型而导致的Write
,我不明白如何做到这一点。