6

我正在尝试使用各种 Scala 实现的类似 C# 的 yield return (即这个)和 "for" -constructions,例如:

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) {

据我了解,我必须将所有代码都设置为类型() => Unit,而不是类型() => Unit @with-annotations。我怎样才能做到这一点?

这个问题似乎很常见,但是我在网上没有找到任何提及。

4

1 回答 1

0

如果您使用iterator链接示例中的类型,您的generate方法是否可能需要具有以下返回类型,而不是您拥有的返回类型?

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

恐怕我对这些东西没有太多经验,但看起来很像您在其中调用的方法iterator必须在注释上有两个(相同的)类型参数。

于 2010-07-20T12:30:28.517 回答