4
假设我有以下类Foo,即使用几倍特技支持任何元数的一个函数:Scala的通用类支撑功能
abstract class Foo[T, R] {
def pull: T => R
}
我可以定义使用以下语法的子类:
implicit def function2Tofunction1[T1, T2, R](f: (T1, T2) => R): ((T1, T2)) => R = {
f.tupled
}
class Moo extends Foo[(Int, Int), Int] {
def pullImpl(x: Int, y:Int):Int = x + y
def pull = (pullImpl _) // implicit converts to tupled form
}
val m = new Moo()
m.pull(4, 5)
这非常笨重。理想的语法将如下所示:
class Moo extends Foo[(Int, Int), Int] {
def pullImpl(x: Int, y:Int):Int = x + y
}
有什么办法来定义我的基类,从而可以做到这一点?