【发布时间】:2023-03-30 23:56:01
【问题描述】:
我是 Scala 的新手,也是高级类型的新手。我想写这样的东西;
trait Actor[E[Dependency] <: Event[Dependency]] {
def execute(dependency: Dependency): Unit
}
但是我不能在execute方法中引用类型参数Dependency - 编译器不知道它。
我知道我可以在没有 HKT 的情况下通过以下方式解决它,但这不是这个问题的目的;
trait Actor[T <: Event[Dependency], Dependency] {
def execute(dependency: Dependency): Unit
}
我想了解为什么它不适用于我尝试过的更高种类的类型语法?是否有可能用 HKT 表达这一点?这是 HKT 的有效用例吗?
编辑
更多信息,Event 看起来像这样;
trait Event[Data] {
val payload: Data
}
...我正在寻找像这样定义一个事件和一个演员;
case class FooEvent(payload: Foo) extends Event[Foo]
class FooActor extends Actor[FooEvent] {
def execute(dependency: Foo) = {}
}
【问题讨论】:
标签: scala generics types higher-kinded-types