indirect enums and structs

Solution 1:

The indirect keyword introduces a layer of indirection behind the scenes.

You indicate that an enumeration case is recursive by writing indirect before it, which tells the compiler to insert the necessary layer of indirection.

From here

The important part of structs and enums is that they're a constant size. Allowing recursive structs or enums directly would violate this, as there would be an indeterminable number of recursions, hence making the size non constant and unpredictable. indirect uses a constant size reference to refer to a constant size enum instance.

There's a different between the two code snippets you show.

  1. The first piece of code makes BinaryTree<T> stored by a reference everywhere it's used.

  2. The second piece of code makes BinaryTree<T> stored by a reference only in the case of node. I.e. BinaryTree<T> generally has its value stored directly, except for this explicitly indirect node case.