假设我有一个简单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]
的显式演员表?如果我忽略它,我会得到一个诊断结果:找到:BinaryTree;需要叶子。Leaf