问题:
关于隐式类的一些东西,使 reduce() 感到困惑。在隐式类中时,编译器会抱怨 reduce() 第二个参数。但是当相同的代码在非隐式方法中时,它可以编译并正常工作。
我对隐式类缺少什么?
代码:
object ImpliCurri {
implicit class MySeq[Int](val l: Seq[Int]) {
//not compiling
final def mapSum(f:Int=>Int):Int = {
l.map(x=>f(x)).reduce(_+_)
//compile error on reduce: Type mismatch. Expected String, fount Int
}
}
// works fine
def mySum(l:Seq[Int], f:Int=>Int):Int = {
l.map(x=>f(x)).reduce(_+_)
// compiles and works no issues
}
}