Scala 树遍历函数的类型问题

Scala 树遍历函数的类型问题,scala,abstract-data-type,Scala,Abstract Data Type,对于我的应用程序正在使用的数据,我试图为数据结构(树)提供一个内置的遍历方法,以便调用者可以简单地调用它,提供在分支行和离开容器上执行的函数。为了允许任何类型T的输出,而不仅仅是树数据上的映射,我让调用方负责连接调用子树的结果,它们必须显式地使用下降回调,该回调传递给提供的rowVisitor函数。我的问题是,我得到了以下错误: Error:(45, 40) missing parameter type for expanded function The argument types of an

对于我的应用程序正在使用的数据,我试图为数据结构(树)提供一个内置的遍历方法,以便调用者可以简单地调用它,提供在分支行和离开容器上执行的函数。为了允许任何类型T的输出,而不仅仅是树数据上的映射,我让调用方负责连接调用子树的结果,它们必须显式地使用下降回调,该回调传递给提供的rowVisitor函数。我的问题是,我得到了以下错误:

Error:(45, 40) missing parameter type for expanded function
The argument types of an anonymous function must be fully known. (SLS 8.5)
Expected type was: T
      def descend(node: Content): T = {
                                      ^
以下是我的数据结构和遍历方法的代码:


通常,该遍历函数将在模板中调用以呈现该内容树,因此T将是Play的Html或字符串

根据评论,以下是更正:

def traverse[T](rowVisitor: (Row, Content => T) => T)(containerVisitor: Container => T): T = {
  def descend(node: Content): T = node match {
    case Row(columns) => rowVisitor(Row(columns), descend)
    case Container(name) => containerVisitor(Container(name))
  }
  descend(contentTree)
}
我还拆分了参数列表,以便调用它,如下所示:

layout.traverse[OutputType] { (row, descend) =>
    /* call descend on each column of the row, and then aggregate to OutputType */
} { container =>
    /* do something with container producing OutputType */
}

在要编译的第一个匿名函数中,似乎需要显式类型参数规范或显式类型。我不确定有没有办法解决这个问题

是否不需要让节点匹配{在下降之后?否则,看起来您正在创建一个匿名函数。的可能重复[.ggovan的意思是你正在创建一个PartialFunction,他是对的——这就是问题所在。你已经声明Descent将返回一个T,但将返回一个PartialFunction。毫无疑问,你的意思是在匹配表达式中使用PartialFunction。@AmigoNico@ggovan:如果你看看我回答中的固定版本,你可能会发现我必须执行当我调用它时,请明确指定遍历的类型参数,或者在匿名函数中指定下降的类型。有什么方法可以避免这种情况吗?
layout.traverse[OutputType] { (row, descend) =>
    /* call descend on each column of the row, and then aggregate to OutputType */
} { container =>
    /* do something with container producing OutputType */
}