扩展类型参数的特征
Trait that extends a type argument
我试过了:
sealed trait AorB
trait A extends AorB { def apiA:... }
trait B extends AorB { def apiB:... }
在另一个文件中:
trait C[AB<:AorB] extends AB
但是得到一个error: class type required but AB found
我真正想做的是说 C
的子类应该实现 A
或 B
(而不是用作某种类型的 AorB
特征枚举,即 A 或 B)。
我可以这样做吗?怎么做?
我在 SO 提出的 "related questions" 中找到了答案(标题不相关:--):
sealed trait AorB
trait A extends AorB { def apiA:... }
trait B extends AorB { def apiB:... }
trait C { this: AorB => }
编辑
我用它来得到一些类型的笛卡尔积之王:
sealed trait CorD { this: AorB => }
trait C extends CorD { this: AorB => def apiC:... }
trait D extends CorD { this: AorB => def apiD:... }
// the "this: AorB =>" need to be repeated
所以(又在其他文件中),我们可以定义:
case class AwithC extends A with C {
def apiA:....
def apiC:....
}
依此类推 AorB x CorD
我试过了:
sealed trait AorB
trait A extends AorB { def apiA:... }
trait B extends AorB { def apiB:... }
在另一个文件中:
trait C[AB<:AorB] extends AB
但是得到一个error: class type required but AB found
我真正想做的是说 C
的子类应该实现 A
或 B
(而不是用作某种类型的 AorB
特征枚举,即 A 或 B)。
我可以这样做吗?怎么做?
我在 SO 提出的 "related questions" 中找到了答案(标题不相关:--):
sealed trait AorB
trait A extends AorB { def apiA:... }
trait B extends AorB { def apiB:... }
trait C { this: AorB => }
编辑
我用它来得到一些类型的笛卡尔积之王:
sealed trait CorD { this: AorB => }
trait C extends CorD { this: AorB => def apiC:... }
trait D extends CorD { this: AorB => def apiD:... }
// the "this: AorB =>" need to be repeated
所以(又在其他文件中),我们可以定义:
case class AwithC extends A with C {
def apiA:....
def apiC:....
}
依此类推 AorB x CorD