首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >需要有关延续的帮助-错误“在非cps位置找到cps表达式”

需要有关延续的帮助-错误“在非cps位置找到cps表达式”
EN

Stack Overflow用户
提问于 2010-04-15 22:21:56
回答 1查看 270关注 0票数 2

我尝试使用Scala2.8 Continuations-PlugIn构建以下简单生成器。下面的错误来自哪里?

代码语言:javascript
复制
None/None/Some((Unit,Unit))
GenTest.scala:8: error: found cps expression in non-cps position
        yieldValue(1)

None/None/Some((Unit,Unit))
GenTest.scala:9: error: found cps expression in non-cps position
        yieldValue(2)

None/None/Some((Unit,Unit))
GenTest.scala:10: error: found cps expression in non-cps position
        yieldValue(3)

代码:

代码语言:javascript
复制
import scala.util.continuations._

object GenTest {

    val gen = new Generator1[Int] {
        yieldValue(1)
        yieldValue(2)
        yieldValue(3)
    }

    def main(args: Array[String]): Unit = {
        for (v <- gen) {
            println(v)
        }
    }
}



class Generator1[E](gen: => Unit @cps[Unit]) {

  var loop: (E => Unit) = null

  def foreach(f: => (E => Unit)): Unit = {
        loop = f
        reset[Unit,Unit]( gen )
  }

  def yieldValue(value: E): Unit @cps[Unit] =
    shift { genK: (Unit => Unit) =>
      loop( value )
      genK( () )
      ()
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-04-15 22:49:06

这些yieldValue调用发生在gen的构造函数中,这是不允许的(我假设)。啊,我只是注意到你打算把它们作为构造函数的参数。好吧,不幸的是,这种语法只适用于方法。我不确定你在这里会不会遇到另一个错误。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2646093

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档