我在一个重度类型系统上,其中一些泛型方法声明为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]
.
有人可以帮我揭示我推理中的缺陷吗?
谢谢