开发者

Implementing Seq[T] for CPS-Classes

Having the following class which is in a CPS-context (@cps[Unit]) how would I implement the Seq-trait? Do I have to leave the standard traits like Seq aside and just implement map, flatmap and foreach in the cps-context?

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
  开发者_如何学JAVA    // dfvar() // not possible as dvar.apply has type "T @cps[Unit]"
    }
  }
}


OK, as far as I got it seems implementing interfaces/traits like Seq is not possible. However as Scala rewrites the for syntactic-sugar-loops into ordinary foreach/map-calls, it works great to just implement map and foreach with the required cps-annotation. filter & co should work as well.

However any advice on how to implement traits in a cps-context is greatly appreciated.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜