Scala Cats FreeMonad - 为什么我的解释器中需要 asInstanceOf[Id[A]]?

Scala Cats FreeMonad - Why do I need asInstanceOf[Id[A]] in my Interpreter?

我有一个使用 cats-free 的 FreeMonads 玩具示例,我的 FunctorTransformer(即解释器)从我的代数(sealed trait StartupActionA[T])到 Id[A] 似乎需要并显式调用 asInstanceOf[Id[A]].

https://github.com/rodoherty1/FreeMonads/blob/master/src/main/scala/io/rob/FreeMonads.scala#L45

根据 Cats documentation,我不需要对 asInstanceOf[Id[A]] 的显式调用。

这是我的代数:

sealed trait StartupActionA[A] extends Product with Serializable
case object StartCluster extends StartupActionA[Unit]
case object StartEventActorShard extends StartupActionA[ActorRef]
case class StartKafka(ref: ActorRef) extends StartupActionA[Option[ActorRef]]

这是我的翻译:

object Interpreter extends (StartupActionA ~> Id) {

  override def apply[A](fa: StartupActionA[A]): Id[A] = fa match {
    case StartCluster =>
      println("Starting up the Akka Cluster").asInstanceOf[A]
    case StartEventActorShard =>
      system.actorOf(Props(new MyActor()), "MyActor").asInstanceOf[A]
    case StartKafka(ref) =>
      Some(ref).asInstanceOf[A]
  }
}

我是否遗漏了隐式转换或者我是否错误地定义了我的代数?

你的代数很好,别被IDEA误导了。

这是一个没有使用 Akka 的小复制,它使用 Scala 2.12.4 和 cats-1.0.0-RC1 编译:

import cats.{Id, ~>}

sealed trait StartupActionA[A] extends Product with Serializable
case object StartCluster extends StartupActionA[Unit]
case object StartEventActorShard extends StartupActionA[String]
case class StartKafka(ref: String) extends StartupActionA[Option[String]]

object Interpreter extends (StartupActionA ~> Id) {
  override def apply[A](fa: StartupActionA[A]): Id[A] = fa match {
    case StartCluster => ()
    case StartEventActorShard => "hello"
    case StartKafka(ref) => Some(ref)
  }
}

尽管 IDEA 用红色波浪线喊叫。