It will take about 3 minutes to finish reading this article.
Enumerations and cases can be marked as indirect, which means that their associated values are stored indirectly, which allows us to define recursive data structures. Example Code
1 2 3 4 5 6 7 8 9 10 11
enumArithmeticExpression { case number(Int) indirectcase addition(ArithmeticExpression, ArithmeticExpression) indirectcase multiplication(ArithmeticExpression, ArithmeticExpression) } // or indirectenumArithmeticExpression { case number(Int) case addition(ArithmeticExpression, ArithmeticExpression) case multiplication(ArithmeticExpression, ArithmeticExpression) }
Call as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
let five =ArithmeticExpression.number(5) let four =ArithmeticExpression.number(4) let sum =ArithmeticExpression.addition(five, four) let product =ArithmeticExpression.multiplication(sum, ArithmeticExpression.number(2))