即使指定了类型,折叠操作中的“递归值X $ 3需要类型”

时间:2018-07-20 16:01:34

标签: scala recursion

对于以下fold调用,我们可以看到已指示每个返回值的类型:

(注意:上面“三”行中显示的内容实际上全部在代码中的第59行上)

val (nRows, dfsOut, dfOut): (Int,DataFrameMap, DataFrame) 
     = (1 to nLevels).foldLeft((0, dfsIn, dfIn)) {
          case ((nRowsPrior, dfsPrior, dfPrior), level) =>
     ..

   (nnRows, dfs, dfOut1)  // These return values are verified as correctly 
                          // matching the listed return types
}

但是我们有以下错误:

Error:(59, 10) recursive value x$3 needs type
val (nRows, dfsOut, dfOut): (Int,DataFrameMap, DataFrame) = (1 to nLevels).foldLeft((0, dfsIn, dfIn)) { case ((nRowsPrior, dfsPrior, dfPrior), level) =>

10表示第一个条目nRows,其设置如下:

 val nnRows = cntAccum.value.toInt  

那绝对是Int ..因此目前尚不清楚根本问题是什么。

(还有另一个类似标题的问题-recursive value x$5 needs type-但是这个问题在输出参数中做了奇怪的事情,而我的是简单的值分配)

2 个答案:

答案 0 :(得分:1)

这是一个没有任何依赖性的MCVE:

trait DataFrameMap
trait DataFrame

val dfsIn: DataFrameMap = ???
val dfIn: DataFrame = ???
val nLevels: Int = 0

val (_, _) = (1, 2)
val (_, _) = (3, 4)
val (nRows, dfsOut, dfOut): (Int,DataFrameMap, DataFrame) = 
  (1 to nLevels).foldLeft((0, dfsIn, dfIn)) {
    case ((nRowsPrior, dfsPrior, dfPrior), level) =>

    val nnRows: Int = nRows
    val dfs: DataFrameMap = ???
    val dfOut1: DataFrame = ???

    (nnRows, dfs, dfOut1)
  }

它准确地再现了错误消息:

  

错误:递归值x $ 3需要输入

val (nRows, dfsOut, dfOut): (Int,DataFrameMap, DataFrame) =
     ^

您必须在nRows主体内部的某个地方使用过dfsOutdfOutfoldLeft。在这里编译就可以了:

trait DataFrameMap
trait DataFrame

val dfsIn: DataFrameMap = ???
val dfIn: DataFrame = ???
val nLevels: Int = 0

val (_, _) = (1, 2)
val (_, _) = (3, 4)
val (nRows, dfsOut, dfOut): (Int,DataFrameMap, DataFrame) = 
  (1 to nLevels).foldLeft((0, dfsIn, dfIn)) {
    case ((nRowsPrior, dfsPrior, dfPrior), level) =>

    val nnRows: Int = ???
    val dfs: DataFrameMap = ???
    val dfOut1: DataFrame = ???

    (nnRows, dfs, dfOut1)
  }

有趣的事实:x$3不是指dfOut(元组的第三部分),而是指整个元组(nRows, dfsOut, dfOut)本身。这就是为什么我必须在(_, _) = ...定义之前添加两个val (nRows, dfsOut, dfOut)才能得到x$3而不是x$1

答案 1 :(得分:0)

问题出在print语句内部:外部foldLeft参数被偶然引用,而不是内部循环参数

    info(s"$tag: ** ROWS ** ${props(OutputTag)} at ${props(OutputPath)} count=$nRows")  

$nRows outer 范围的变量:这将导致递归。本来打算引用$nnRows