以下代码显示类型不匹配错误:
def f(arr:List[Int]): List[Int] =
for(num <- 0 to arr.length-1; if num % 2 == 1) yield arr(num)
Run Code Online (Sandbox Code Playgroud)
据说它找到了IndexedSeq而不是List。以下作品:
def f(arr:List[Int]): List[Int] =
for(num <- (0 to arr.length-1).toList; if num % 2 == 1) yield arr(num)
Run Code Online (Sandbox Code Playgroud)
我i <- a to b之前在for循环中使用过,但之前没有看到此错误。有人可以解释为什么i <- a to b不能在此处使用该格式吗?
scala ×1