为CPS类实现Seq [T]

时间:2010-04-19 13:27:03

标签: scala scala-2.8 continuations

如果在CPS上下文中有以下类(@cps [Unit]),我将如何实现Seq-trait? 我是否必须将Seq等标准特征放在一边,只需在cps-context中实现map,flatmap和foreach?

class DataFlowVariable[T] {
  def apply(): T @cps[Unit] = ...
}

class DataFlowStream[T] extends Seq[T] {

  override def iterator: Iterator[T] = new Iterator[T] {
    private val iter = queue.iterator
    def hasNext: Boolean = iter.hasNext
    def next: T = { // needed: next: T @cps[Unit] !
      val dfvar = iter.next
      // dfvar() // not possible as dvar.apply has type "T @cps[Unit]"
    }
  }
}

1 个答案:

答案 0 :(得分:1)

好吧,据我所知,似乎无法实现像Seq这样的接口/特性。 但是,当Scala将for句法 - 糖循环重写为普通的foreach / map-calls时,只需使用所需的cps-annotation实现mapforeach即可。 过滤器&合作应该也可以。

然而,非常感谢任何有关如何在cps-context中实现特征的建议。