[英]Trait that extends a type argument
I tried that: 我尝试过:
sealed trait AorB
trait A extends AorB { def apiA:... }
trait B extends AorB { def apiB:... }
and in another file: 并在另一个文件中:
trait C[AB<:AorB] extends AB
But get an error: class type required but AB found
但是得到一个
error: class type required but AB found
What I actually want to do is to say that subclasses of C
should implements either A
or B
(and not AorB
which is used as some kind of trait-Enum, ie either A or B). 我真正想做的是说
C
子类应实现A
或B
(而不是AorB
用作某种特征Enum的AorB
,即A或B)。
Can I do that, and how? 我可以这样做吗?
I found the answer in one of the "related questions" proposed by SO (which had an unrelated title :-): 我在SO提出的“相关问题”之一中找到了答案(该问题的标题不相关:-):
sealed trait AorB
trait A extends AorB { def apiA:... }
trait B extends AorB { def apiB:... }
trait C { this: AorB => }
I use this to have some king of cartesian product of types: 我用它来拥有一些笛卡尔积类型的国王:
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
So (in other files again), we can define: 因此(再次在其他文件中),我们可以定义:
case class AwithC extends A with C {
def apiA:....
def apiC:....
}
And so on with any combination of AorB x CorD
以此类推,以
AorB x CorD
任意组合
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.