abstract BinaryTree
サブクラスNode
を持つ単純ながありLeaf
、 を生成する関数を書きたいとしますList[Leaf]
。
def getLeaves(tree: BinaryTree): List[Leaf] =
tree match {
case Leaf(v) => List(tree.asInstanceOf[Leaf])
case Node(left, right) => getLeaves(left) ++ getLeaves(right)
}
ケースasInstanceOf[Leaf]
で明示的なキャストを回避する方法はありますか? 省略した場合、次のような診断メッセージが表示されます: found: BinaryTree; 葉が必要です。Leaf