【问题标题】:Mixing in a path dependent trait混合路径依赖特征
【发布时间】:2015-09-21 15:38:02
【问题描述】:

因此,我想将这些不同的特征混合到一个名为 GPState(遗传编程状态)的基类中。然而,我想要混合的一些东西取决于在构建 GPState 之前我不会知道的东西。因此我使用了 scala 的路径依赖特性。

假设我的路径依赖特征看起来像这样

case class HasTermGen[...](depths : Seq[Int], widths : Seq[Int]) {
  trait gen extends GPState[...] {
    override def genInitTerm(): Term = {
      <...some stuff here depending on 'depths' and 'widths'...>
    }
  }
}

现在,当我想构建一个 GPState 时,我希望能够做这样的事情

val gpstate = new GPState[Var, Type, Term, Double] 
              with <...some mixin...>
              <...some other mixins that do other stuff...>
              with HasTermGen[...](3 to 7, 4 to 9).gen

但这似乎是一个语法错误。如果我先定义一个变量,我就可以做到

val hasTermGen = HasTermGen[...](3 to 7, 4 to 9)
val gpstate = new GPState[Var, Type, Term, Double] 
              with <...some mixin...>
              <...some other mixins that do other stuff...>
              with hasTermGen.gen

但这看起来有点丑陋。

我愿意放弃这种特定的语法,甚至使用像“HasTermGen”这样的类和许多其他东西。我真正需要的是能够创建一个对象,该对象基于在施工现场定义的信息以特定方式实现 GPState 的方法。此外,实现这些方法的方法很可能有一些他们想要使用的共同元素(数据方面),因此也需要加以考虑。有没有更好的方法来做到这一点?

【问题讨论】:

    标签: scala mixins traits dependent-type path-dependent-type


    【解决方案1】:

    您的特征 GPState 取决于 HasTermGen 的实例。

    val myTermGen = HasTermGen[...](3 to 7, 4 to 9)
    val myState = new myTermGen[...] { ... }
    

    【讨论】:

    • 对不起,我不明白你想在这里表达什么。介意再解释一下吗?
    猜你喜欢
    • 1970-01-01
    • 2015-12-23
    • 2013-06-20
    • 1970-01-01
    • 2012-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多