【发布时间】:2019-10-07 08:21:37
【问题描述】:
我在一个重度类型系统上,其中一些泛型方法声明为def execute]C <: A#C](cmd: C):EitherT[Future, Fail, Seq[A#E]](其中A 是类的泛型类型。
这很好用。但是在我的测试中,当我模拟这些调用时,我必须明确键入 Fail 和 A#E 的超类型,否则我的代码无法编译。
// Event is the base type of Receiver#E
val event:Event = SubOfEvent()
handler.execute(any[SubOfCommand]) returns Seq(event).asRightT[Future, Fail]
val fail:Fail = SubOfFail()
handler.execute(any[SubOfCommand]) returns fail.asLeftT[Future, Seq[Receiver#E]]
如果我内联 event 或 fail 的声明,我有一个类型不匹配:
found : cats.data.EitherT[scala.concurrent.Future,SubOfFail,scala.collection.immutable.Seq[SubOfEvent]]
required: cats.data.EitherT[scala.concurrent.Future,Fail,scala.collection.immutable.Seq[Receiver#E]]
(which expands to) cats.data.EitherT[scala.concurrent.Future,Fail,scala.collection.immutable.Seq[Event]]
Note: SubOfFail <: Fail, but class EitherT is invariant in type A.
You may wish to define A as +A instead. (SLS 4.5)
handler.execute(any[SubOfCommand]) returns SubOfFail().asLeftT[Future,
^
我理解关于EitherT 在类型A 中不变的消息。但我期待它能够将EitherT[F, SubOfA, B] 翻译为EitherT[F, SubOfA.asInstanceOf[A], B]。
有人可以帮我揭示我推理中的缺陷吗?
谢谢
【问题讨论】:
标签: scala scala-cats mockito-scala