Consider this enum in Swift:
enum Labels: String, CaseIterable {
case foo
case bar
case baz
}
The CaseIterable
conformance is automatically synthesized by the compiler. But if I add a new case with availability annotation, this conformance is not automatically synthesized:
// Type ‘Labels’ does not conform to protocol ‘CaseIterable’
enum Labels: String, CaseIterable {
case foo
case bar
case baz
@available(iOS 14, *)
case notAvailableYet
}
This is easy to get around by implementing allCases
manually for these types and only including values that are available on the current OS version. Is the lack of automatic synthesis a shortcoming of the compiler, or is this an intentional choice? I can see an argument for allCases
as not being semantically correct for the case where not all cases are available. But I can also see a pragmatic approach where using allCases
would benefit from including all available cases.