I'm learning with the book "Functional Swift", and there's a code snippet I don't understand.
indirect enum Tree <Element: Comparable> {
case Leaf
case Node(Tree <Element>, Element, Tree <Element>)
}
extension Tree {
init() {
self = .Leaf
}
init(_ value: Element) {
self = .Node(.Leaf, value, .Leaf)
}
var isEmpty: Bool {
if case .Leaf = self {
return true
}
return false
}
}
let a = Tree<Int>()
let b = Tree<Int>(5)
a.isEmpty
b.isEmpty
It 's if case .Leaf = self that confused me. Why case is not in the switch block, and why it uses assignment(=) rather than equation(==) in condition statement. I know it means if this enumeration value is .Leaf then blablabla, but the syntax I really don't understand.