【发布时间】:2019-03-16 22:37:49
【问题描述】:
我很困惑为什么我不能从以下 Scala 代码行中生成集合:
val pairs = for( v <- vc; o <- oc) yield (v,o)
它在这个函数的内部,它使用不可变向量。
private def recMergeVirtualAndReal(mCell: RCell, vc: Vector[Cell], oc: Vector[Cell]): Vector[Cell] = {
var temp_oc = oc
val pairs = for( v <- vc; o <- oc) yield (v,o)
val newVCells =
for((left, right) <- pairs if left contains right) yield {
temp_oc = temp_oc.filterNot(o => o == left || o == right)
captureVCells(left,right,mCell)
}
if(newVCells.nonEmpty) recMergeVirtualAndReal(mCell, recMergeVirtualCells(newVCells ++ vc), temp_oc)
else vc
}
我在堆栈跟踪中收到以下错误:
Exception in thread "main" java.lang.ClassCastException: scala.collection.immutable.Vector cannot be cast to game.Cell
at model.Board$$anonfun$10.apply(Board.scala:223)
at scala.collection.immutable.List.flatMap(List.scala:327)
我很困惑,因为我没有尝试转换任何东西,它只是一个没有任何类型转换的简单语句。
我也试过这个以防编译器无法理解类型:
val pairs = for( v: Cell <- vc: Vector[Cell]; o: Cell <- oc: Vector[Cell]) yield (v: Cell,o: Cell)
【问题讨论】:
-
这不是真正的正确minimal reproducible example。我无法将它粘贴到我的 REPL 中并运行它,因为缺少东西。这就是为什么我只能在我的回答中猜测你的问题。
-
223是哪一行?
标签: scala vector functional-programming classcastexception immutable-collections