您如何使用 Scala 将功能混合到迭代过程的每个步骤中?

How do you mixin functionality to each step of an iterative procedure with Scala?

我正在研究 Scala 中的优化过程,正在寻求有关如何构建我的问题的建议。该过程一次采取一个步骤,所以我天真地使用 Step class:

对问题进行了建模
class Step(val state:State) {
  def doSomething = ...
  def doSomethingElse = ...
  def next:Step = ... // produces the next step in the procedure
}

过程中的每个步骤都由不可变 Step class 表示,其构造函数被赋予上一步产生的状态,其 next 方法产生后续 Step 实例。基本思想是用 Iterator[Step] 包装它,以便可以采取步骤直到优化收敛。虽然有点简单,但这对普通情况很有效。

然而,现在我需要为算法添加各种扩展,并且我需要根据要优化的问题任意混合这些扩展。通常这将通过可堆叠特征模式来完成,但这种方法会为这个问题带来问题。这是两个可能的扩展的示例:

trait FeatureA extends Step {
  // Extension-specific state passed from step to step
  val aState:FeatureAState = ...

  // Wrap base methods to extend functionality
  abstract override def doSomething = { ...; super.doSomething(); ... }
}

// Just like Feature A
trait FeatureB extends Step {
  val bState:FeatureBState = ...
  abstract override def doSomething = { ...; super.doSomething(); ... }
}

有时优化需要 FeatureA 混合,其他时候 FeatureB,有时两者都需要。

主要问题是基础class的next方法不知道混入了哪些扩展,因此后续生成的步骤不会将任何扩展混入初始步骤.

此外,每个扩展程序都需要逐步传递自己的状态。在这个例子中 FeatureAState/FeatureBState 实例被包含在各自的特征中,但是没有覆盖 next 方法, FeatureAFeatureB 没有办法传递他们独特的状态。无法在每个特征中覆盖 next,因为可能混合了这些扩展的组合,并且每个都只知道自己。

看来我把自己逼到了一个角落,希望有人对如何使用 Scala 处理这个问题有一些见解。哪种设计模式最适合此类问题?

您可能有兴趣探索 F-bound polymorphism 模式。此模式允许您定义 return 特征或基 class 中的当前子类型的方法。这是您示例的简化版本:

trait Step[T <: Step[T]] { self: T =>
    val name: String
    def next: T
}

case class BasicStep(name: String) extends Step[BasicStep] {
    def next = this.copy(name = name + "I")
}

case class AdvancedStep(baseName: String, iteration: Int) extends Step[AdvancedStep] {
    val name = s"$baseName($iteration)"
    def advancedFunction = println("foobar")
    def next = this.copy(iteration = iteration + 1)
}

所以我们已经定义了基本的 Step 特征,它有一个 name 和一个 next 方法,return 无论自我类型是什么扩展 class。例如,BasicStep return 中的 next 方法是 BasicStep。这允许我们根据需要迭代和使用特定于子类型的覆盖:

val basicSteps = Iterator.iterate(BasicStep("basic"))(_.next).take(3).toList
//basicSteps: List[BasicStep] = List(BasicStep(basic), BasicStep(basicI), BasicStep(basicII))

val advancedSteps = Iterator.iterate(AdvancedStep("advanced", 0))(_.next).take(3).toList
//advancedSteps: List[AdvancedStep] = List(AdvancedStep(advanced,0), AdvancedStep(advanced,1), AdvancedStep(advanced,2))
val names = advancedSteps.map(_.name)
//names: List[String] = List(advanced(0), advanced(1), advanced(2))
advancedSteps.last.advancedFunction
//foobar

如果你想像这样混合多种类型,不幸的是你不能使用泛型(你会收到一个 "inherits different type instances of trait" 错误)。但是,您可以使用抽象类型成员来表达 F-bound 多态性:

trait Step { self =>
    type Self <: Step { type Self = self.Self }
    val name: String
    def next: Self
}

trait Foo extends Step {
    val fooMarker = "foo"
}
trait Bar extends Step {
    val barMarker = "bar"
}

case class FooBar(name: String) extends Foo with Bar {
    override type Self = FooBar
    def next = this.copy(name + "I")
}

然后 FooBar 个实例将具有 FooBar 上的方法:

val fooBar = FooBar("foobar").next.next
fooBar.barMarker //"bar"
fooBar.fooMarker //"foo"
fooBar.name //"fooNameII"

注意这个名字来自Foo,因为它是第一个混入的。