我的印象是,这
// short syntax
def foo(bar: Bar)(baz: Baz): Quux
是语法糖
// long syntax
def foo(bar: Bar): (Baz) => Quux
但在继承方面,我似乎无法将两者混为一谈。整个树必须以短句法或长句法定义;从不两者兼而有之。
例如:
case class Context
case class Work
trait ContextualWorker {
def workWithContext(ctxt: Context)(work: Work): Traversable[Work]
}
class ShortConcreteWorker extends ContextualWorker {
override def workWithContext(ctxt: Context)(work: Work) = Nil
}
class LongConcreteWorker extends ContextualWorker {
// error on next line: method workWithContext overrides nothing <-------------
override def workWithContext(ctxt: Context): (Work) => Traversable[Work] = {
val setupCode = 1
{ work => Nil }
}
}
如果我将特征更改为使用长语法,则 ShortConcreteWorker 不会编译。
有没有理由说明这些不能互换/继承?你是怎么解决的?
现在最灵活的方法似乎是用长语法定义树,也许委托给 ShortConcreteWorker 中的实现类,如下所示:
case class Context
case class Work
trait ContextualWorker {
def workWithContext(ctxt: Context): (Work) => Traversable[Work]
}
class ShortConcreteWorker extends ContextualWorker {
override def workWithContext(ctxt: Context) = workWithContextImpl(ctxt)_
private def workWithContextImpl(ctxt: Context)(work: Work) = Nil
}
class LongConcreteWorker extends ContextualWorker {
override def workWithContext(ctxt: Context): (Work) => Traversable[Work] = {
val setupCode = 1
{ work => Nil }
}
}