Enum's with only one single case used as associated value and Mirror

When trying to use Mirror in combination with an enum that only has a single case that is an associated value from another enum, I ran into a surprising case of Mirror working differently for enums with one case and enums with more than one case:

enum OnlyOneCase {
  case theOneAndOnlyCase
}

enum TwoCases {
  case first, second
}

enum Wrapper {
  case foo(OnlyOneCase)
  case bar(TwoCases)
}

let itemA = Wrapper.foo(.theOneAndOnlyCase)
let itemB = Wrapper.bar(.first)

print(Mirror(reflecting: itemA).children.first) // prints nil
print(Mirror(reflecting: itemB).children.first) // prints: Optional((label: Optional("bar"), value: Project.TwoCases.first))

I would have expected the first print to have printed prints: Optional((label: Optional("foo"), value: Project. OnlyOneCase.theOneAndOnlyCase)), but instead it prints nil.

Is this intended behaviour or a bug or a side effect of some optimisation for enums with single cases? If it's intended, is there a rational behind it?

1 Like
Terms of Service

Privacy Policy

Cookie Policy