Using round parens around the second param list heals the code too and might be less surprising:
object TestProgressTool extends App {
val x = new ProgressTool(1000,.01) ({ x:Double =>
println("Hello "+ x)
})
}
题
the code below runs perfectly fine if I simply make it a "case class". However, I certainly do not want that for a mutable object - but by changing it to a regular class, it no longer seems to understand the anonymous function parameter at the end. Not sure why I can't get past this, i've tried a few variations and can't understand why making it a case class works. (scala 2.10.2) - thanks
/**
*
* @param target - can be any large target number
* @param perPiece - at what percentage interval is todo called
* @param todo - passed current percentage complete
*/
class ProgressTool(target:Double, perPiece:Double) (todo: (Double)=>Unit) extends Mutable {
private[this] var lastUpdate:Double =0.0
def update(atPos:Double) = {
val step = atPos - lastUpdate
if (step/target >=perPiece) {
lastUpdate += step
todo(lastUpdate)
}
}
}
object TestProgressTool extends App {
val x = new ProgressTool(1000,.01) { x:Double =>
println("Hello "+ x)
}
}
missing arguments for constructor ProgressTool in class ProgressTool val x = new ProgressTool(1000,.01) { x:Double => ^
解决方案 2
Using round parens around the second param list heals the code too and might be less surprising:
object TestProgressTool extends App {
val x = new ProgressTool(1000,.01) ({ x:Double =>
println("Hello "+ x)
})
}
其他提示
Not sure why it seems to work, but try this (note extra parentheses around constructor with first arguments):
object TestProgressTool extends App {
val x = (new ProgressTool(1000,.01)) { x:Double =>
println("Hello "+ x)
}
}