let's say I wanted to have something like the following:
abstract class PDF[T, S <: PDF[T, _]] {
def fit(obs: Seq[T], weights: Seq[Double]): S
}
class PDFGaussian(val mu: Double, val Sigma: Double) extends PDF[Double, PDFGaussian] {
def fit(obs: Seq[Double], weights: Seq[Double]): PDFGaussian =
new PDFGaussian(...) // bla bla bla
}
So, basically, what I want is to have the fit
function to return an instance of the type of its enclosing class which obviously must be a subclass of PDF[T]
.
However, instead of having to use the double parameterization PDF[T, S <: PDF[T, _]]
I'd rather go with only one type parameter like so:
abstract class PDF[T] {
def fit[S <: PDF[T]](obs: Seq[T], weights: Seq[Double]): S
}
class PDFGaussian(val mu: Double, val Sigma: Double) extends PDF[Double] {
def fit[S <: PDF[_]](obs: Seq[Double], weights: Seq[Double]): S =
new PDFGaussian(...) // bla bla bla
}
If I do this, however, the compiler yells at me for returning PDFGaussian
instead of S
. Since I'm obviously missing some important fact about scala's type system here could you please clarify what I'm doing wrong and show me how to do it with only one type parameter?