6

I'm trying to use various Scala implementations of C#-like yield return (i.e. this one) with "for" -constructions such as:

private def permutations[T](s: Vector[T]) = {
  def swap(i: Int, j: Int) {
    val tmp = s(i)
    s.set(i, s.get(j))
    s.set(j, tmp)
  }

  iterator[Vector[T]] {
    def generate(left: Int, right: Int): Unit @cps[Iteration[Vector[T]]] = {
      if (left >= right)
        yieldValue(s)

      else {
        generate(left, right)
        for (i <- left to right) {
          swap(left, i)
          generate(left+1, right)
          swap(left, i)
        }
      }
    }

    generate(0, s.size-1)
  } 
}

But this code compiles with error:

error: no type parameters for method foreach: (f: (Int) => U)Unit exist so that it can be applied to arguments ((Int) => Unit @util.continuations.package.cps[ru.ispras.texterra.nlp.GHMMDisambiguator.Iteration[Vector[T]]])
--- because ---
argument expression's type is not compatible with formal parameter type;
found   : (Int) => Unit @util.continuations.package.cps[ru.ispras.texterra.nlp.GHMMDisambiguator.Iteration[Vector[T]]]
required: (Int) => ?U
for (i <- left to right) {

As I understand I have to make all code inside for to be type of () => Unit, not of () => Unit @with-annotations. How can I do that?

This problem seems to be very common, but I didn't found any mentions in the Internet.

Community
  • 1
  • 1
sbos
  • 307
  • 4
  • 11
  • 1
    Maybe I'm missing something, but the braces on that example don't seem to match up. And how are you able to call generate there? It's in a nested scope. – Kevin Wright Sep 07 '10 at 10:06
  • I solved both problems by removing an extra brace. – Blaisorblade Apr 23 '12 at 01:25
  • This question is a duplicate of the following, which provides an answer: http://stackoverflow.com/questions/8934226/continuations-and-for-comprehensions-whats-the-incompatibility – Urban Vagabond Jul 26 '12 at 16:32

1 Answers1

0

If you're using the iterator type from the linked example, is it possible that your generate method needs to have the following return type rather than the one you have there?

Unit @cps[Iteration[Vector[T]],Iteration[Vector[T]]]

I'm afraid I haven't got much experience with this stuff but it looks a lot like the methods you call within iterator must have two (identical) type arguments on the annotation.

Calum
  • 5,846
  • 2
  • 26
  • 22
  • http://www.scala-lang.org/docu/files/api/scala/util/continuations/package.html : type cps = cpsParam[A, A] So I think scala team had renamed cps to cpsParam and added type cps Even if I use @cps[Iteration[Vector[T]],Iteration[Vector[T]]] the problem remains – sbos Jul 21 '10 at 07:03
  • Oh well, sorry! I was just working from what I saw there. Good luck getting it working! – Calum Jul 22 '10 at 22:09