sbo*_*bos 6 continuations scala yield yield-return scala-2.8
我正在尝试使用各种Scala实现的C#类似的yield return(即这一个)和"for" - 这样的构造:
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)
}
}
Run Code Online (Sandbox Code Playgroud)
但是这段代码编译错误:
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) {
Run Code Online (Sandbox Code Playgroud)
据我所知,我必须使所有代码成为() => Unit,而不是() => Unit @with-annotations.我怎样才能做到这一点?
这个问题似乎很常见,但我没有在互联网上找到任何提及.
如果您使用iterator链接示例中的类型,您的方法是否可能generate需要具有以下返回类型,而不是您那里的返回类型?
Unit @cps[Iteration[Vector[T]],Iteration[Vector[T]]]
Run Code Online (Sandbox Code Playgroud)
恐怕我对这些东西没有太多经验,但看起来很像您调用的方法iterator在注释上必须有两个(相同的)类型参数。