Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
      // dfvar() // not possible as dvar.apply has type "T @cps[Unit]"
    }
  }
}
like image 822
hotzen Avatar asked Apr 19 '10 13:04

hotzen


1 Answers

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.

like image 63
hotzen Avatar answered Oct 06 '22 00:10

hotzen