7

在 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]"
    }
  }
}
4

1 回答 1

1

好的,据我所知,似乎Seq不可能实现接口/特征。然而,由于 Scala 将for语法糖循环重写为普通的 foreach/map 调用,因此只需实现mapforeach使用所需的 cps-annotation 就可以很好地工作。filter & co 也应该可以工作。

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

于 2010-05-13T17:56:36.907 回答