【发布时间】:2021-08-13 02:41:03
【问题描述】:
我正在 cat.free.Free Monad 库的帮助下使用案例类编写 DSL。 DSL 将由接收消息的 Actor 进行解释,因此每个 Actor 必须首先使用 Free.resume 解包命令。
这在六年前使用 scalaz where we also used the resume function 也很有效,但是免费 monad 的 Functor 是 easy to create,因为我们使用了带有额外函数参数的案例类,可以映射到诸如 k下面。
case class GetResource[Rdf <: RDF, A](
uri: Rdf#URI,
k: NamedResource[Rdf] => A) extends LDPCommand[Rdf, A]
但是当前的cats.scala.Free on the cats Free Monad web page 的例子并没有这样的论点。确实,当通过自然转换使用 Free Monads 的解释时,这些效果很好。我用一个案例类尝试了with a super simple DSL
sealed trait LDPCmd[A]:
def url: Uri
case class Get[T](url: Uri) extends LDPCmd[Response[T]]
然后我可以为此编写a simple Script,它可以按预期工作in the tests using the natural transformation interpretation。
但是使用基于 Actors 的解释,我现在需要解开 free monad 中的每个命令,这些命令使用 Free 的 resume 函数发送给不同的 Actor。这需要一个仿函数。但是我不清楚仿函数在哪里可以得到控制。即,我在 ???位置在这里
given CmdFunctor: cats.Functor[LDPCmd] with
def map[A, B](fa: LDPCmd[A])(f: A => B): LDPCmd[B] = fa match
case g: Get => ???
【问题讨论】:
-
添加了 Scala 3 标签
标签: scala scalaz scala-cats scala-3