为 CPS 类实现 Seq[T]
拥有 CPS 上下文 (@cps[Unit]) 中的以下类,我将如何实现 Seq 特征? 我是否必须将 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]"
}
}
}
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]"
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,据我所知,实现像
Seq
这样的接口/特征似乎是不可能的。然而,由于 Scala 将
for
语法糖循环重写为普通的 foreach/map 调用,因此只需实现map
和foreach
就可以很好地工作所需的 cps 注释。过滤器和co 也应该工作。
然而,任何有关如何在 cps 上下文中实现特征的建议都非常感谢。
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 implementmap
andforeach
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.