我試圖定義,定義如下二叉樹匹配:斯卡拉圖案選項類型
trait Node {
val label: Int
}
case class BranchNode(override val label: Int, left: Option[Node], right: Option[Node]) extends Node
case class LeafNode(override val label: Int) extends Node
,然後使用Scala的模式如下匹配定義一個簡單的printTree
方法:
def printTree(aTree: Option[Node]): Unit = aTree match {
case None => print(".")
case Some(LeafNode(label)) => print(label)
case Some(BranchNode(label, left, right)) => "{" + printTree(left) + label + printTree(right) + "}"
}
Intellij IDE警告我說這個匹配可能並不完整。 Option
可以有None
或Some
作爲值。在Option[Node]
的情況下,它可以是Some(LeafNode)
或Some(BranchNode)
。我還忽略了什麼其他案例?