English 中文(简体)
与依附方法类型等同的电文
原标题:Message equivalent of dependent method types

感谢"这个 后我脑海环绕依赖方法类型。我有一个类似于以下结构的结构 。

trait Environment{
    type Population <: PopulationBase
    protected trait PopulationBase

    def evolveUs(population: Population): Population
}

object FactoredOut{
    def evolvePopulation(env: Environment)(prevPopulation: env.Population): env.Population = {
        env.evolveUs(prevPopulation)
    }
}

我现在要开始使用行为主体来将工作在 FactoredOut 部分中传播到一个组。 要做到这一点,我需要一个方法来传递带有 Environment 的不可改变的信件。

显然<它们> 以下的内容不起作用 , 但显示我试图做的事情

object Messages{
    case class EvolvePopulation(env: Environment)(prevPopulation: env.Population)
}

如何正确通过人口和周围环境?

(本会添加依赖型方法标签, 但我没有足够的点数来添加新标签)

最佳回答

您的直觉需要将依赖型号( env. point )的值和该型号作为单一对象所依赖的值( env )包装起来,这是完全正确的。

鉴于你已经公布的定义 最简单的方法可能是这样

// Type representing the packaging up of an environment and a population
// from that environment
abstract class EvolvePopulation {
  type E <: Environment
  val env : E
  val prevPopulation : env.Population
}

object EvolvePopulation {
  def apply(env0 : Environment)(prevPopulation0 : env0.Population) =
    new EvolvePopulation {
      type E = env0.type
      val env : E = env0 // type annotation required to prevent widening from
                         // the singleton type
      val prevPopulation = prevPopulation0
    }
}

现在如果我们定义一个具体的环境类型,

class ConcreteEnvironment extends Environment {
  class Population extends PopulationBase
  def evolveUs(population: Population): Population = population
}

我们可以像以前一样直接使用它,

val e1 = new ConcreteEnvironment

val p1 = new e1.Population
val p2 = e1.evolveUs(p1)
val p3 = e1.evolveUs(p2)

并且我们也可以把一个环境和人口 包装起来进行分配,

def distrib(ep : EvolvePopulation) {
  import ep._
  val p4 = env.evolveUs(prevPopulation)
  val p5 = env.evolveUs(p4)
  val p6 = env.evolveUs(p5)
}

val ep1 = EvolvePopulation(e1)(p3)

distrib(ep1)
问题回答

暂无回答




相关问题
How to flatten a List of different types in Scala?

I have 4 elements:List[List[Object]] (Objects are different in each element) that I want to zip so that I can have a List[List[obj1],List[obj2],List[obj3],List[obj4]] I tried to zip them and I ...

To use or not to use Scala for new Java projects? [closed]

I m impressed with Twitter and investigating to use Scala for a new large scale web project with Hibernate and Wicket. What do you think about Scala, and should I use it instead of Java? EDIT: And, ...

Why does Scala create a ~/tmp directory when I run a script?

When I execute a Scala script from the command line, a directory named "tmp" is created in my home directory. It is always empty, so I simply deleted it without any apparent problem. Of course, when I ...

Include jar file in Scala interpreter

Is it possible to include a jar file run running the Scala interpreter? My code is working when I compile from scalac: scalac script.scala -classpath *.jar But I would like to be able to include a ...

Scala and tail recursion

There are various answers on Stack Overflow which explain the conditions under which tail recursion is possible in Scala. I understand the limitations and how and where I can take advantage of tail ...

热门标签