gpt4 book ai didi

scala - 错误 : Covariant type A occurs in contravariant position

转载 作者:行者123 更新时间:2023-12-04 11:44:01 24 4
gpt4 key购买 nike

我试图写一个不可变的 Matrix[A]类(class)。我希望该类在 A 上是协变的但是当我把 +前面A编译器开始提示类中的某些操作。

以下是我的 Matrix 的相关子集类(实际类比以下子集大 5 倍):

class Matrix[+A] private(val contents: Vector[Vector[A]])(implicit numericEv: Numeric[A])
extends ((Int, Int) => A) with Proxy {

import numericEv._
import Prelude._

// delegate `equals` and `hashCode` implementations to `contents`
override def self = contents

val nRows: Int = contents.length

val nColumns: Int = contents(0).length.ensuring { len =>
contents.forall(_.length == len)
}

def dimensions = (nRows, nColumns)

def isSquare = nRows == nColumns

def hasSameOrderAs[B : Numeric](that: Matrix[B]) = this.dimensions == that.dimensions

def isComformableWith[B : Numeric](that: Matrix[B]) = this.nColumns == that.nRows

private def assertSameOrder[B : Numeric](that: Matrix[B]) {
assert(this.hasSameOrderAs(that), "Matrices differ in dimensions.")
}

private def assertIsSquare() {
assert(this.isSquare, "Not a square matrix.")
}

def zipWith[B : Numeric, C : Numeric](that: Matrix[B])(f: (A, B) => C): Matrix[C] = {
assertSameOrder(that)
val zippedContents = (contents, that.contents).zipped.map((v1, v2) => (v1, v2).zipped.map(f))
Matrix(zippedContents)
}

def map[B : Numeric](f: A => B): Matrix[B] = {
Matrix(contents.map(_.map(f)))
}

def transpose: Matrix[A] = {
assertIsSquare()
Matrix(contents.transpose)
}

def +(that: Matrix[A]): Matrix[A] = this.zipWith(that)(_ + _)

def -(that: Matrix[A]): Matrix[A] = this.zipWith(that)(_ - _)

def *(scalar: A): Matrix[A] = this.map(_ * scalar)

def *(that: Matrix[A]): Matrix[A] = {
assert(this.isComformableWith(that))
Matrix.tabulate(this.nRows, that.nColumns) { (r, c) =>
(this(r), that.transpose(c)).zipped.map(_ * _).sum
}
}
}

object Matrix {
def apply[A : Numeric](rows: Vector[A]*): Matrix[A] = Matrix(Vector(rows: _*))

def apply[A : Numeric](contents: Vector[Vector[A]]): Matrix[A] = new Matrix(contents)

def tabulate[A : Numeric](nRows: Int, nColumns: Int)(f: (Int, Int) => A): Matrix[A] = {
Matrix(Vector.tabulate(nRows, nColumns)(f))
}
}

编译器为类中的最后四个操作显示错误“协变类型 A 出现在逆变位置”。我无法理解这些错误的原因,以及如何摆脱它。请解释这些错误背后的原因并提出解决方法。谢谢。

最佳答案

出现这些错误的原因是,按照您的方式声明它不是类型安全的。例如,可以这样做,否则:

class A(val x: Int)
class B(x: Int, val y: Int) extends A(x)

object NA extends Numeric[A] {
def toDouble(x: A): Double = x.x.toDouble
def toFloat(x: A): Float = x.x.toFloat
def toLong(x: A): Long = x.x.toLong
def toInt(x: A): Int = x.x
def fromInt(x: Int): A = new A(x)
def negate(x: A): A = new A(-x.x)
def times(x: A,y: A): A = new A(x.x * y.x)
def minus(x: A,y: A): A = new A(x.x - y.x)
def plus(x: A,y: A): A = new A(x.x + y.x)
def compare(x: A,y: A): Int = implicitly[Numeric[Int]].compare(x.x, y.x)
}

object NB extends Numeric[B] {
def toDouble(x: B): Double = x.x.toDouble / x.y.toDouble
def toFloat(x: B): Float = x.x.toFloat / x.y.toFloat
def toLong(x: B): Long = (x.x / x.y).toLong
def toInt(x: B): Int = x.x / x.y
def fromInt(x: Int): B = new B(x, 1)
def negate(x: B): B = new B(-x.x, x.y)
def times(x: B,y: B): B = new B(x.x * y.x, x.y * y.y)
def minus(x: B,y: B): B = new B(x.x * y.y - y.x * x.y, x.y * y.y)
def plus(x: B,y: B): B = new B(x.x * y.y + y.x * x.y, x.y * y.y)
def compare(x: B,y: B): Int = implicitly[Numeric[Int]].compare(x.x * x.y, y.x * y.y)
}

val mb = Matrix.tabulate(10, 10)((x, y) => new B(x, y))
def f(m: Matrix[A]) = {
val ma = Matrix.tabulate(m.nRows, m.nColumns)((x, y) => 1)
m + ma
}
f(mb)

请注意 m + ma无法工作,因为 m.+期望类型为 B 的对象.如果 Scala 允许您按照您的方式编写它,那么这将是允许的。

避免这个问题的常见方法是编写这样的方法:
def +[B >: A](that: Matrix[B])(implicit num: Numeric[B]): Matrix[B] = this.zipWith(that)(B.plus)

关于scala - 错误 : Covariant type A occurs in contravariant position,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5241501/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com