hot*_*zen 7 continuations scala scala-2.8
如果在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]"
}
}
}
Run Code Online (Sandbox Code Playgroud)
好吧,据我所知,实现这样的接口/特征似乎Seq是不可能的。然而,由于 Scala 将for语法糖循环重写为普通的 foreach/map 调用,因此只需实现map并foreach使用所需的 cps 注释就可以很好地工作。过滤器和公司应该也能工作。
然而,任何有关如何在 cps 上下文中实现特征的建议都将受到高度赞赏。
| 归档时间: |
|
| 查看次数: |
410 次 |
| 最近记录: |