我一直在尝试部分应用功能,并且发生了这样的事情。假设我们有这样的代码:
class Color(name:String) {
override def toString = name
}
class Point(x: Int, y:Int) {
override def toString:String = "("+x +"," + y + ")"
}
class Linestyle(name: String) {
override def toString = name
}
def drawLine(width:Double, color: Color, style: Linestyle, a:Point, b: Point): Unit = {
println("Draw " + width + " " + color + " " + style + " " + " line from point: " + a + " to point " + b)
}
当我尝试以这种方式创建仅采用 4 个参数的 drawSolidLine 函数时:
def drawSolidLine (c: Color, a:Point, b: Point):Unit =
drawLine(1.0, _:Color, new Linestyle("SOLID"), _:Point, _:Point)
并尝试调用它
drawSolidLine(2.5, new Color("Black"),new Point(2,4), new Point(3,1))
我没有编译器错误,但调用没有返回任何内容。
另一方面,当我以这种方式创建 drawSolidLine 时:
val drawSolidLine = drawLine(_:Double, _:Color, new Linestyle("SOLID"),
_:Point, _:Point)
并在之前调用它,我有所需的输出:
Draw 1.0 Black SOLID line from point: (2,4) to point (3,1)
我错过了什么?