/// `indirect enum` 让枚举可以递归包含自己,等价于 C 的 tagged union + 指针。 public indirect enum Expr: Sendable, Equatable { case number(Int) case add(Expr, Expr) case multiply(Expr, Expr) public var value: Int { switch self { case .number(let number): return number case .add(let lhs, let rhs): return lhs.value + rhs.value case .multiply(let lhs, let rhs): return lhs.value * rhs.value } } public var depth: Int { switch self { case .number: return 1 case .add(let lhs, let rhs), .multiply(let lhs, let rhs): return 1 + max(lhs.depth, rhs.depth) } } public var description: String { switch self { case .number(let number): return "\(number)" case .add(let lhs, let rhs): return "(\(lhs.description) + \(rhs.description))" case .multiply(let lhs, let rhs): return "(\(lhs.description) * \(rhs.description))" } } }