次のコードは、再帰呼び出しの戻り値の型が Any である限りコンパイルされますが、Any である必要はないため、明らかに何か間違っています。
case class Group(
id: Long = -1,
parentId: Long = -1,
name: String = "")
def makeTree(groupId: Long, groups: List[Group]) = {
def getAllChildren(gid: Long): Any = {
def children = for {
g <- groups; if g.parentId == gid
} yield g
if (children.isEmpty) List()
else {
children map { x =>
getAllChildren(x.id)
}
}
}
getAllChildren(groupId)
}
val groups = List(Group(1, 0, "A"),
Group(2, 1, "B"),
Group(3, 1, "C"),
Group(4, 2, "D"))
makeTree(1, groups)
//Results in: Any = List(List(List()), List())
}
getAllChildren の署名を次のように変更すると:
def getAllChildren(gid: Long): List[Group]
次に、エラーが発生します。
type mismatch; found : List[List[Group]] required: List[Group]
ここで何が間違っていますか。