为 CPS 类实现 Seq[T]

发布于 2024-08-29 18:18:03 字数 549 浏览 4 评论 0原文

拥有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡淡離愁欲言轉身 2024-09-05 18:18:03

好吧,据我所知,实现像 Seq 这样的接口/特征似乎是不可能的。
然而,由于 Scala 将 for 语法糖循环重写为普通的 foreach/map 调用,因此只需实现 mapforeach 就可以很好地工作所需的 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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文