scala - 避免名称与 Cake Pattern 冲突

标签 scala traits cake-pattern name-clash

我目前正在使用 Cake Pattern 来实现一些优化算法。我经常遇到名称冲突问题。例如:

trait Add[T] { this: Foo[T] =>
  def constant: T
  def plus( t1: T, t2: T ): T
  def add( t: T ) = plus( t, constant )
}

trait Mul[T] { this: Bar[T] =>
  def constant: T
  def times( t1: T, t2: T ): T
  def mul( t: T ) = times( t, constant )
}

trait Operations[T] { this: Add[T] with Mul[T] =>
  def neg( t: T ): T
}

在这里,constantAdd 中都定义了和 Mul特质,但它们的值(value)可能不同。我可以用特征名称作为名称的前缀,但我发现它丑陋而脆弱( def mulConstant: T )。有没有更好的方法呢?

最佳答案

据我所知,传统的蛋糕模式通常涉及 1 层特征嵌套,将操作组合在一起。然后,外层声明了实际的“服务”(这里是:Add、Mul、Operations)而不定义它。

trait AddComponent[T] { this: FooComponent[T] =>
  def addition: Add

  trait Add {
    def constant: T
    def plus( t1: T, t2: T ): T
    def add( t: T ) = plus( t, constant )
  }
}

trait MulComponent[T] { this: BarComponent[T] =>
  def multiplication: Mul

  trait Mul {
    def constant: T
    def times( t1: T, t2: T ): T
    def mul( t: T ) = times( t, constant )
  }
}

trait OperationsComponent[T] { this: Add[T] with Mul[T] =>
  def operations: Operations

  trait Operations {
    def neg( t: T ): T
  }
}

然后,当将“...Component”特征混合在一起时,依赖关系被连接起来:
trait IntOperations extends Operation[Int] {
  class IntAdd extends Add { ... }
  class IntMul extends Mul { ... }
}

class MyFooBar extends FooComponent[Int] with BarComponent[Int] with IntOperations {
  lazy val addition = new IntAdd
  lazy val multiplication = new IntMul
  lazy val foo = ...
  lazy val bar = ...
}

这解决了您特定的命名空间问题,但名称冲突(“服务”定义)仍然是传统蛋糕模式的问题。有一个blog post作者:Daniel Spiewak 演示了一般如何解决这个问题,但该解决方案有其自己的一组(巨大的)权衡(参见 this talk)。

希望有所帮助。

附言在这里使用抽象类型而不是类型参数可能会更好

关于scala - 避免名称与 Cake Pattern 冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16519664/

相关文章:

scala - 当涉及特征时,Mockito 会忽略我的 Specs2 加糖验证步骤

scala - 使用蛋糕模式的传递 DI

scala - 如何以orc格式在已创建的配置单元表中加载excel数据

closures - 将修改其环境的闭包传递给 Rust 中的函数

rust - 是否可以专注于静态生命周期?

struct - Rust 结构无法替换 HashMap 中的特征

scala - 蛋糕图案 : How to share an instance?

javascript - 如果您使用 SBT 0.10 或更高版本,如何缩小 Javascript

sql - 如何使用 Spark Scala 或 sql 对特定时间间隔内的记录进行分组?

mysql - 使用 slick 将 MySQL 连接到 Scala