【发布时间】:2010-11-29 11:01:29
【问题描述】:
我想要一个密封的特征,它有一个声明的方法,它返回 扩展特征的实际类。我应该使用抽象类型、参数类型还是 有没有其他好的方法来解决这个问题?
sealed trait Foo {
type T
def doit(other: T): T
}
或
sealed trait Foo[T] {
def doit(other: T): T
}
请注意,T 在此示例中必须是 Foo 的子类型。如果我这样做,类型
信息感觉过于重复:
case class Bar(name: String) extends Foo[Bar] {
def doit(other: Bar): Bar = ...
}
【问题讨论】:
标签: scala