【发布时间】:2021-09-04 12:39:43
【问题描述】:
我正在尝试将 Either 作为使用 Free monad 的代数的结果,如以下代码
///// Command
sealed trait Command[A]
type Result[A] = Either[Exception, A]
object Command {
case class Tell(line: String) extends Command[Result[Unit]]
case class Ask(line: String) extends Command[Result[String]]
}
type App[A] = EitherK[Command, Log, A]
def program(implicit L: LogOps[App],
C: CommandOps[App]): Free[App, Unit] =
for {
_ <- L.show("start <ask>")
name <- C.ask("What's your name?")
_ <- L.show("start <tell>")
_ <- C.tell(s"Hi <$name>, nice to meet you!")
_ <- L.show("done.")
} yield ()
...
问题是名字是一个 Either,所以我得到了以下输出
L--- start <ask>
What's your name?
George
L--- start <tell>
Hi <Right(George)>, nice to meet you!
L--- done.
有什么想法吗?
谢谢
【问题讨论】:
标签: scala scala-cats either free-monad