Need help with Continuations-Error "found cps expression in non-cps position"

I am trying to create the following simple generator using Scala 2.8 Continuations-PlugIn. Where does the following error come from?

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)

      

code:

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( () )
      ()
    }
}

      

+2


a source to share


1 answer


Those calls are yieldValue

happening inside the constructor gen

, which is illegal (I'm guessing). Ah, I just noticed that you intend them to be a constructor parameter. Well, unfortunately this syntax only works with methods. I'm not sure if you won't get another error here.



+1


a source







All Articles