scala - 家庭多态性+混合?

标签 scala mixins traits

我有一个类型家族,我想使用 mixin 以模块化方式“丰富”它们。例如:

trait Family {
  self =>
  trait Dog {
    def dogname:String
    def owner:self.Person
  }
  trait Person {
    def name:String
    def pet:self.Dog
  }
}

trait SerializableFamily extends Family {
  trait Dog extends super.Dog {
    def toSimpleString:String = "Dog(" + dogname + ")"
  }
  trait Person extends super.Person {
    def toSimpleString:String = "Person(" + name + ") and his pet " + pet.toSimpleString
  }
}

trait SerializableFamily2 extends Family {
  trait Dog extends super.Dog {
    def toLoudString:String = "Dog(" + dogname.toUpperCase + ")"
  }
  trait Person extends super.Person {
    def toLoudString:String = "Person(" + name.toUpperCase + ") and his pet " + pet.toLoudString
  }
}

但是,上面的方法不起作用(Scala 2.9.1)。最后一个表达式编译失败(pet.toSimpleString)。

这只是我从我尝试过的几种策略中随机选择的一种:自打字、抽象类型、 super [...]等。

我希望最终能够做这样的事情:

val family = new Family with SerializableFamily with TraversableFamily with FooFamily {}

其中每个 mixin 向家族中的一个或多个类型添加一组协作方法。

这是一个常见的模式,我已经看到通过使用隐式包装器、基于模式匹配的访问者等来解决。但是由于它只是常规混合模式的递归应用,我想知道是否有更简单的方法来实现

最佳答案

您的案例中的错误是预料之中的,因为混合中的 DogPerson 不会覆盖 DogFamily 中的 Person,因此 self.Person 仍然指代 Family.Person

这可能更接近你想要的

trait Family {
  // type DogType = Dog won't work because then two different mixins 
  // have incompatible DogType implementations
  type DogType <: Dog
  type PersonType <: Person

  trait Dog {
    def dogname:String
    def owner:PersonType 
  }
  trait Person {
    def name:String
    def pet:DogType 
  }
}

trait SerializableFamily extends Family {
  type DogType <: Dog
  type PersonType <: Person

  trait Dog extends super.Dog {
    def toSimpleString:String = "Dog(" + dogname + ")"
  }
  trait Person extends super.Person {
    def toSimpleString:String = "Person(" + name + ") and his pet " + pet.toSimpleString
  }
}

但是你有一些令人讨厌的东西,比如

new Family with SerializableFamily with TraversableFamily with FooFamily {
  type DogType = super[SerializableFamily].Dog with super[TraversableFamily].Dog with super[FooFamily].Dog
}

关于scala - 家庭多态性+混合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14703607/

相关文章:

java构造函数无法转换为scala

node.js - Jade ,mixin && html

Ruby 动态模块混入

rust - 为什么 Ord 特征不能使用 cmp 函数为继承特征的所需方法提供默认实现?

c++ - 当只有少数成员存在差异时,是否有必要对整个类(class)进行特化?

java - Scala 检查外部进程 ID (pid) 当前是否正在运行?

基于 Scala 约束的类型和文字

Scala classOf 用于类型参数

ruby - 模块的实例变量是否在类与 mixin 之间共享?

scala - 在 Scala Trait 中使用 self 类型作为返回类型