在特征之间共享通用

MintyAnt

我有两个特点

trait Base[A]

trait SometimesUsedWithBase[A] {
    this: Base[A] =>
}

然后,我将它们与一个类一起使用

class StringThing extends Base[String] with SometimesUsedWithBase[String]

如果我不必定义SomethingUsedWithBase的类型,那就太好了,相反,它以某种方式理解它正在使用Base中定义的类型,因此它看起来像:

class StringThing extends Base[String] with SometimesUsedWithBase

这可能吗?

斯图尔特·麦克韦恩

您应该能够执行这样的操作。

trait Base[A] {
  type BaseType = A
}

trait SometimesUsedWithBase {
  this: Base[_] =>
  def someFunction: BaseType
}

class StringThing extends Base[String] with SometimesUsedWithBase {
  def someFunction: String = ""
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章