Who benefits from the `indirect` keyword?

Only the designer could answer this. That said, recursive would be incorrect as you can mark any case as indirect so long as you deem it beneficial/necessary. Recursive cases are just the obvious necessary ones.

A slightly less obvious case is when you have a very large struct that you rarely use:

enum SomeEnum {
  case a, b
  indirect case c(LargeStruct)
}

Recall that the size of the enum is the size of the largest case (+discriminator). So if we don't use indirect, the size of SomeEnum would be about the size of LargeStruct. However, if the common cases are a and b, that would be very wasteful. So we add indirect to make the size of SomeEnum about the size of a pointer. When case c is used, the compiler will allocate a separate region for LargeStruct.

6 Likes