以下不编译:
package play
object Stats2 {
def variance(data: Seq[Double], dof: Int = 0): Double = {
println("variance Double direct"); 1.0
}
def variance[T](data:Seq[T], dof: Int = 0)(implicit ex: T => Double): Double = {
println("variance Double extracted"); 1.0
}
}
编译器说:
$ scalac erasure2.scala
erasure2.scala:7: error: double definition:
method variance$default$2:[T]=> Int and
method variance$default$2:=> Int at line 4
have same type after erasure: ()Int
def variance[T](data:Seq[T], dof: Int = 0)(implicit ex: T => Double): Double = {
^
one error found
如果将dof :Int = 0更改为dof: Int,则示例编译并按预期工作。
无论代码是否有效,默认参数值的存在与否都会改变,这似乎很难看。
为什么这真的有意义的解释是什么?