我正在尝试在scala中执行以下操作:
def addPoints(x1: Int, y1: Int)(x2: Int, y2: Int)(x3: Int, y3: Int) = {
doSomethingWith(x1, y1, x2, y2, x3, y3)
(addPoints _)
}
但是,我正在努力弄清楚这个函数的返回类型应该是什么,因为似乎不可能定义一个无限递归的函数类型。我希望能够做到的是:
type RecursiveFunction2[A, B] = (A, B) => RecursiveFunction2[A, B]
var pointAdder: RecursiveFunction2[Int, Int] = (addPoints _)
def onEvent(x: Int, y: Int) {
pointAdder = pointAdder(x, y)
}
但是,类型无法引用自己。有没有办法实现这一点,而不是诉诸Any
的返回类型,并调用asInstanceOf[Function2[Int, Int, Any]]
?