扩展类型参数的特性 [英] Trait that extends a type argument

查看:82
本文介绍了扩展类型参数的特性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试过:

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(而不是实现某种特征-Enum的AorB,即A或B) ).

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, i.e. either A or B).

我可以那样做吗?

推荐答案

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

I found the answer in one of the "related questions" proposed by SO (which had an unrelated title :-):

sealed trait AorB
trait A extends AorB { def apiA:... }
trait B extends AorB { def apiB:... }

trait C { this: AorB => }

编辑

我用它来拥有某种类型的笛卡尔积:

Edit

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:....
}

AorB x CorD

这篇关于扩展类型参数的特性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆