【发布时间】:2018-06-08 19:16:31
【问题描述】:
我有一个通用案例类 Route,它接受 Location 的子类列表。但是,在以下方法中,我在对 distance expected: head.T, actual: T 的调用中出现类型不匹配的情况@
case class Route[T <: Location](route: List[T]) {
def measureDistance: Double = {
def measure(head: T, tail: List[T], acc: Double = 0.0): Double = tail match {
case Nil => acc
case h :: t => measure(h, t, head.distance(h) + acc)
}
if (route.isEmpty) 0.0
else measure(route.head, route.tail)
}
}
基本的抽象Location类如下
abstract class Location(val name: String) {
type T <: Location
def distance(that: T): Double
}
由于 head 和 h 都来自同一个列表 route 我不明白为什么它们不是同一类型。
【问题讨论】:
标签: scala generics types f-bounded-polymorphism