繁体   English   中英

扩展类型参数的特性

[英]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子类应实现AB (而不是AorB用作某种特征Enum的AorB ,即A或B)。

我可以这样做吗?

我在SO提出的“相关问题”之一中找到了答案(该问题的标题不相关:-):

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任意组合

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM