我目前有:如何删除这个额外的类型参数?
class X[+T: Numeric](val x: T)
abstract class M[N: Numeric, T <: X[N]] { // <- I'd like to remove N.
def apply(x: Int): T
final def row = (1 to 10).map(this(_))
}
我用这样的:
class Y(x: Double, val y: Double) extends X[Double](x)
class Z extends M[Double, Y] { // <- So that this is simpler.
def apply(x: Int) = new Y(0.0, 0.0)
}
它的工作原理是这样的:
object testapp {
// row is properly polymorphic, allowing access to Y.y
def main(args: Array[String]): Unit = (new Z).row.map(r => println(r.y))
}
我想Z
要简单一些,这样我可以使用M
如:
class Z extends M[Y] {
def apply(x: Int) = new Y(0.0, 0.0)
}
,或者甚至更好:
class Z extends M[Double] { // i.e. Meaning apply can return
def apply(x: Int) = new Y(0.0, 0.0) // any subclass of X[Double]
}
Here are my Gist iterations达到这一点。
从最直接回答问题开始。谢谢。你能为我澄清吗?我正在阅读它:'type W <:X [A]'在'M'内定义了一个'W'类型,使得'W'是'X [A]'的子类型。那么在'M'的子类型中,'W'被“覆盖”成为'M'子类型所需的类型? – nix
随着界限,类型W仍然是抽象的,所以当你定义它时不需要覆盖。 (像任何其他成员一样。)(Spec 4.3。同样,要小心http://stackoverflow.com/a/10223364/1296806。) –