【发布时间】:2016-09-22 18:39:21
【问题描述】:
trait Account[T <: Account[T]]
case class BrokerAccount(total:BigDecimal) extends Account[BrokerAccount]
case class SavingsAccount(total:BigDecimal) extends Account[SavingsAccount]
下面的函数声明和调用工作正常。
def foo1( xs: Array[T forSome { type T <: Account[T] }]):Array[T forSome { type T <: Account[T] }] = xs
foo1(Array(BrokerAccount(100),SavingsAccount(50)))
但下面的调用给出了编译错误。
def foo2( xs: List[T forSome { type T <: Account[T] }]):List[T forSome { type T <: Account[T] }] = xs
foo2(List(BrokerAccount(100),SavingsAccount(50)))
错误
Main.scala:14: 错误:类型不匹配;
找到:列表[带有可序列化的 Main.Account 的产品 [_ >:带有 Main.BrokerAccount 的 Main.SavingsAccount :带有 Main.BrokerAccount 的 Main.SavingsAccount 必需:List[T forSome { type T <: main.account foo2>
有人可以解释一下为什么在后面的情况下会出现编译错误吗?
【问题讨论】: