trait Account[T <: Account[T]]
case class BrokerAccount(total:BigDecimal) extends Account[BrokerAccount]
case class SavingsAccount(total:BigDecimal) extends Account[SavingsAccount]
Описание и описание функций ниже.Scala F-Bounded Type Polymorphism
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: error: type mismatch;
found : List[Product with Serializable with Main.Account[_ >: Main.SavingsAccount with Main.BrokerAccount <: Product with Serializable with Main.Account[_ >: Main.SavingsAccount with Main.BrokerAccount <: Product with Serializable]]]
required: List[T forSome { type T <: Main.Account[T] }] foo2(List(BrokerAccount(100),SavingsAccount(50)))
Может кто-то пожалуйста, объясните мне, почему ошибка компиляции происходит в последнем случае?
Как может компилироваться ниже строка? foo2 (Список [T forSome {type T <: Account [T]}] (BrokerAccount (100), SavingsAccount (50))) –
Для получения дополнительной информации: http://stackoverflow.com/questions/6684493/why-are- массивы-инвариантные-но-списки-ковариантные –
Спасибо Сергею. –