我目前有:如何刪除這個額外的類型參數?
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。) –