我试图找出scala协方差和逆变。可能会在两个不同的概念之间混淆。 处理以下代码:
sealed trait Algorithm[ +T <: Model, P <: Model, R <: AnyVal] {
def name: String
def train(trainingData: DenseMatrix[Double]): T
def predict(row: DenseVector[R], mlModel : P): R
}
然后我有两种声明为:
的算法类型case class LibLinear() extends Algorithm[Active_Linear, Active_Linear, Double] {
override val name = "libLinear"
override def train(trainingData: DenseMatrix[Double]): Active_Linear = {
........
}
override def predict(row: DenseVector[Double], model: Active_Linear): Double = {
..........
}
}
case class SVM() extends Algorithm[Volume_SVM, Volume_SVM, Double] {
override val name = "libSVM"
override def train(trainingData: DenseMatrix[Double]): Volume_SVM = {
..........
}
override def predict(row: DenseVector[Double], model: Volume_SVM): Double = {
...........
}
}
Active_Linear
和Volume_SVM
都是Model
的子类型。
现在我不能这样做:
val algorithm: Algorithm[Model, Model, Double] = SVM()
SVM
是Algorithm
的子类型,Volume_SVM
是Model
的子类型。我们使用Algorithm
和covariant
符号声明contravariant
。
答案 0 :(得分:2)
那是因为算法只在涉及T
时是协变的。 P
和R
被定义为不变量;您必须根据需要在每个+
或-
之前添加,然后相应地修改代码。根据你的最终作业,我做了一些假设,这就是我的想法:
sealed trait Algorithm[ +T <: Model, +P <: Model, +R <: AnyVal] {
def name: String
def train(trainingData: DenseMatrix[Double]): T
def predict[U >: R, V >: P](row: DenseVector[U], mlModel : V): U
}
case class LibLinear() extends Algorithm[Active_Linear, Active_Linear, Double] {
override val name = "libLinear"
override def train(trainingData: DenseMatrix[Double]): Active_Linear = {
...
}
override def predict[U >: Double, V >: Active_Linear](row: DenseVector[U], model: V): U = {
...
}
}
case class SVM() extends Algorithm[Volume_SVM, Volume_SVM, Double] {
override val name = "libSVM"
override def train(trainingData: DenseMatrix[Double]): Volume_SVM = {
...
}
override def predict[U >: Double, V >: Volume_SVM](row: DenseVector[U], model: V): U = {
...
}
}
你的上一个作业然后工作正常。