Scala implementation of C # -like output with "for"

I am trying to use various Scala implementations of C # -shaped return returns (ie this one ) with "for" - constructs 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 an 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 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 it, I have to do all the code inside to be of type () => Unit , not () => Unit @with-annotations . How can i do this?

This problem seems very common, but I did not find any references on the Internet.

+6
yield scala yield-return continuations
source share
1 answer

If you use the iterator type from the linked example, is it possible that your generate method should have the next return type, and not the one you have there?

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

I am afraid that I do not have much experience with this material, but it is very similar to the methods that you call inside the iterator , they should have two (identical) argument types in the annotation.

0
source share

All Articles