私は、次のような"for" 構文を使用して、C# のような yield return (つまり、これ) のさまざまな Scala 実装を使用しようとしています。
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)
}
}
しかし、このコードはエラーでコンパイルされます:
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) {
私が理解しているように、 for 内のすべてのコードを() => Unit
ではなく の型にする必要があり() => Unit @with-annotations
ます。どうやってやるの?
この問題は非常に一般的なようですが、インターネットで言及されているものは見つかりませんでした。