11

I'm trying to experiment with recursive enums in Swift 2 however I'm getting compilation errors.

I started off trying to define my own example:

enum Tree {
    case Empty
    indirect case Node(value: Int, left: Tree, right: Tree)
}

But get an error: "Consecutive declarations on a line must be separated by :".

So, I tried Apple's own example from their WWDC15 What's New in Swift presentation:

enum Tree<T> {
  case Leaf(T)
  indirect case Node(Tree, Tree) 
}

But its the same compilation error with this also. If I create a new playground and paste these lines in then it results in the error - see screenshot, or if in an Xcode project same thing, see other screenshot.

I'm using Xcode 7.0.

How come I can't even get Apple's example to compile?

enter image description here enter image description here

Jojodmo
  • 23,357
  • 13
  • 65
  • 107
Gruntcakes
  • 37,738
  • 44
  • 184
  • 378
  • 4
    If I remember correctly, they said it's coming but not there yet – Juan Jun 21 '15 at 17:22
  • 4
    right, in that talk Lattner mentioned that the 'indirect' keyword didn't make it into Beta 1 (but is coming soon) – fqdn Jun 21 '15 at 17:24

1 Answers1

16

According to the release notes, support for this was added in Xcode 7 beta 4, which states:

Enums and cases can be marked indirect, which causes the associated value for the enum to be stored indirectly, allowing for recursive data structures to be defined.

The following code works in a Playground:

enum Tree {
    case Empty
    indirect case Node(value: Int, left: Tree, right: Tree)
}

let tree1 = Tree.Node(value: 0, left: Tree.Empty, right: Tree.Empty)
let tree2 = Tree.Node(value: 0, left: Tree.Node(value: -1, Tree.Empty, Tree.Empty), right: Tree.Empty)

Anecdotally, trying to use the enum with a switch worked fine, but using the new Swift 2 if case syntax repeatedly crashed Xcode and made the Playground unusable. I'm not sure if this is related specifically to enums or just general beta instability.


Background:

At the time this question was originally asked and this answer accepted, Xcode beta1 was the latest release. Xcode 7 beta1—beta3 did not support this and their release notes contained the following verbiage:

“indirect” enum elements are not yet implemented yet in this beta, they will be added in a later update.

Brett
  • 4,341
  • 2
  • 19
  • 17
  • Thanks for the update about Xcode 7 beta 2. Was wondering about it. – Michael Welch Jun 24 '15 at 13:30
  • Is it possible to get the parent for a given node? – user965972 Oct 21 '15 at 13:26
  • Not as given. I suppose you could add a `parent` member to Node if you made it mutable, but this feels weird to me. See this discussion on implementing a doubly-linked list in Haskell, which is a similar problem: http://stackoverflow.com/questions/10386616/how-to-implement-doubly-linked-lists -- If you are processing a `Tree` recursively you should have a reference to the parent and child nodes before you recurse, so you may just need to change your algorithm a bit. If you need a fuller discussion try posting a question. – Brett Oct 21 '15 at 20:17