I'm trying to test swift enums by extending the ArithmeticExpression from the example with variables but can't get it to work. Any help appreciated:
enum ArithmeticExpression: CustomStringConvertible {
case number(Int)
case variable(Character)
indirect case addition(ArithmeticExpression, ArithmeticExpression)
indirect case multiplication(ArithmeticExpression, ArithmeticExpression)
func eval() -> ArithmeticExpression {
switch self {
case .number(let n):
return self
case .variable(let c):
return self
case .addition(.number, .number): // HERE I want to access the numbers as e1 and e2
return ArithmeticExpression.number(e1 + e2) // Won't work because e1 and e2 are not defined
case let .multiplication(e1, e2): // SAME HERE
return ArithmeticExpression.number(e1 * e2)
}
}
}