But it looks like you want F-bounded polymorphism:trait GameStatistics[G <: GameStatistics[G]] { this: G => def equity: Double def add(s: G): G def multiply(x: Double): G}object GameStatistics { def aggregate[G <: GameStatistics[G]](stats: Seq[G]): G = stats.reduce( _ add _ )}case class SimpleGameStatistics(equity: Double, nrGames: Int) extends GameStatistics[SimpleGameStatistics] { override def add(s: SimpleGameStatistics): SimpleGameStatistics = SimpleGameStatistics((equity * nrGames + s.equity * s.nrGames) / (nrGames + s.nrGames), nrGames + s.nrGames) override def multiply(x: Double): SimpleGameStatistics = SimpleGameStatistics(equity * x, nrGames)} 这篇关于如何在Scala中将方法参数限制为子类类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
06-23 11:07