Implementing Seq[T] for CPS-Classes
Asked Answered
M

1

7

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]"
    }
  }
}
Mercantile answered 19/4, 2010 at 13:27 Comment(1)
Is my question too cryptic? To sum it up: As all functions working in a CPS-context need to return a type annotated with cps[X], I am not able to implement any standard Traits/Interfaces like Seq because their signature lacks these @cps-annotations. Is this correct?Mercantile
M
1

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.

Mercantile answered 13/5, 2010 at 17:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.