# Access control for enum cases

**URL:** <https://forums.swift.org/t/access-control-for-enum-cases/47142>\
**Category:** Pitches\
**Tags:** enums, pattern-matching\
**Created:** [April 4, 2021, 7:52am UTC](https://forums.swift.org/t/access-control-for-enum-cases/47142 "2021-04-04T07:52:24Z")\
**Posts on this page:** 1\
**Showing post:** 17

<div class="post-metadata">

**Author:** ![typesanitizer](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/typesanitizer/32/8661_2.png) [@typesanitizer](https://forums.swift.org/u/typesanitizer)\
**Post date:** [April 5, 2021, 2:29pm UTC](https://forums.swift.org/t/access-control-for-enum-cases/47142/17 "2021-04-05T14:29:30Z")

</div>

> [@nicklockwood](#):
>
> I would suggest that rather than requiring a `default:` clause, the use of `@unknown` clauses should be extended to support this use case, so that even within the same module I could exhaustively switch over public/internal cases of an enum with private/fileprivate cases.

I didn't mention it explicitly but this use case should work, since `@unknown default:` is a variation of `default:`. Put another way, a `default:` clause is required, but if one adds `@unknown`, then it would behave analogous to how `@unknown` behaves in other situations; it should warn about any cases that are accessible but not explicitly written.

> [@nicklockwood](#):
>
> I also don't see any particular reason to restrict the use of CaseIterable and RawRepresentable. There are legitimate reasons why it's useful to be able to refer to private cases indirectly, such as in unit test suites.

I understand the use case for testing. However, conformances for externally defined protocols need to be public, so it gives a very easy way for clients to create/iterate over non-public cases, which may be undesirable. My impression from Matthew's [prior pitch](https://forums.swift.org/t/access-control-for-enum-case-initializers/22663/31) was that this was unappealing. Certainly open to removing this restriction though.

One potential workaround if you're using only `internal` cases is that you could hand-write a retroactive conformance in the test suite after doing a `@testable import`; that would prevent library clients from accessing the conformance, while allowing the conformance to be used for easier testing. That's still more clunky though than the alternative of having compiler-synthesized conformances.

---

_[View the full topic](https://forums.swift.org/t/access-control-for-enum-cases/47142)._
