下面的代碼將不能編譯,爲什麼不斯卡拉與泛型類的情況下密封延伸特質
class FruitProcessor[T <: Fruit] {
def process(fruit: T) = {
// Do something with fruit
}
}
class FruitBlender[T <: Fruit] {
val fruitProcessor = new FruitProcessor[T]
def blend(fruit: T) = {
fruit match {
case b: Banana => fruitProcessor.process(b)
case a: Apple => fruitProcessor.process(a)
}
}
}
sealed trait Fruit
case class Banana(id: String) extends Fruit
case class Apple(id: String) extends Fruit
的編譯錯誤是沿着線的我不明白原因「發現香蕉,需要T」
我在這裏做錯了什麼?
放下'T',並將其替換爲'Fruit'。 – Reactormonk