Swift enum recursive related value

I am trying to create a mathematical expression using Enumeration in Swift. This enumeration can be a constant with an associated value of type ComplexNumber (simple structure). It can also be a square root expression with a recursive associated value. For example, I want to be able to store sqrt (sqrt (1 + 2i)) in an enumeration.

enum Expression {
    case Sqrt(Expression)
    case Constant(ComplexNumber)
}

Xcode (6 beta 2) will work immediately. What is the problem? From what I read in the Swift tutorial about related values, this should work.

+4
source share
1 answer

Now possible with Swift 2.0 beta 4. Examples taken from release notes:

 enum List<T> {
   case Nil
   indirect case Cons(head: T, tail: List<T>)
 }

 indirect enum Tree<T> {
   case Leaf(T)
   case Branch(left: Tree<T>, right: Tree<T>)
 }
+1

Source: https://habr.com/ru/post/1545261/


All Articles