Scala for-comprehension类型推断

Obs*_*ver 5 scala for-comprehension

下一个代码

  def f(chars: List[Char]): List[List[Char]] = chars match {
    case Nil => List(Nil)
    case x :: xs => for {
      v <- f(xs)
    } yield List(x) :: v
  }
Run Code Online (Sandbox Code Playgroud)

给出错误消息

- type mismatch;  found   : List[List[Any]]  required: List[List[Char]]
Run Code Online (Sandbox Code Playgroud)

请帮助我理解为什么'for'选择最一般的Any而不是Char?我应该阅读语言规范中的哪个主题?谢谢.

fol*_*one 10

结果,你yielding是一个混合List[List[List[Char]]]List[List[Char]].斯卡拉向上倾斜List[List[Any]].对于您的情况,以下任何一种都可以完成这项工作:

scala>  def f(chars: List[Char]): List[List[Char]] = chars match {
     |     case Nil => List(Nil)
     |     case x :: xs => for {
     |       v <- f(xs)
     |     } yield x :: v
     |   }
f: (chars: List[Char])List[List[Char]]

scala>  def f(chars: List[Char]): List[List[Char]] = chars match {
     |     case Nil => List(Nil)
     |     case x :: xs => for {
     |       v <- f(xs)
     |     } yield List(x) ++ v
     |   }
f: (chars: List[Char])List[List[Char]]
Run Code Online (Sandbox Code Playgroud)


Dan*_*ral 6

问题是List(x)- 它需要x.

首先,v迭代结果f(xs),然后f返回List[List[Char]].这意味着结果将是List[X],X返回的类型在哪里yield.

类型vList[Char],因为它迭代的内容f(xs).所以我们必须弄清楚它的类型List(x) :: v,它的前缀是List[Char]a List[Char].它没有连接它们:它将列表添加到仅包含字符的列表中.结果列表中包含两个CharList[Char]其中.

由于唯一满足两者的类型Any,然后X将是Any和理解的结果List[Any].