<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">I’ve been thinking a lot about our public access modifier story lately in the context of both protocols and enums. &nbsp;I believe we should move further in the direction we took when introducing the `open` keyword. &nbsp;I have identified what I think is a promising direction and am interested in feedback from the community. &nbsp;If community feedback is positive I will flesh this out into a more complete proposal draft.</div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">Background and Motivation:</div><div class=""><br class=""></div>In Swift 3 we had an extended debate regarding whether or not to allow inheritance of public classes by default or to require an annotation for classes that could be subclassed outside the module. &nbsp;The decision we reached was to avoid having a default at all, and instead make `open` an access modifier. &nbsp;The result is library authors are required to consider the behavior they wish for each class. &nbsp;Both behaviors are equally convenient (neither is penalized by requiring an additional boilerplate-y annotation).<div class=""><br class=""></div><div class="">A recent thread (<a href="https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20170206/031566.html" class="">https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20170206/031566.html</a>) discussed a similar tradeoff regarding whether public enums should commit to a fixed set of cases by default or not. &nbsp;The current behavior is that they *do* commit to a fixed set of cases and there is no option (afaik) to modify that behavior. &nbsp;The Library Evolution document (<a href="https://github.com/apple/swift/blob/master/docs/LibraryEvolution.rst#enums" class="">https://github.com/apple/swift/blob/master/docs/LibraryEvolution.rst#enums</a>) suggests a desire to change this before locking down ABI such that public enums *do not* make this commitment by default, and are required to opt-in to this behavior using an `@closed` annotation.</div><div class=""><br class=""></div><div class="">In the previous discussion I stated a strong preference that closed enums *not* be penalized with an additional annotation. &nbsp;This is because I feel pretty strongly that it is a design smell to: 1) expose cases publicly if consumers of the API are not expected to switch on them and 2) require users to handle unknown future cases if they are likely to switch over the cases in correct use of the API.</div><div class=""><br class=""></div><div class="">The conclusion I came to in that thread is that we should adopt the same strategy as we did with classes: there should not be a default.</div><div class=""><br class=""></div><div class="">There have also been several discussions both on the list and via Twitter regarding whether or not we should allow closed protocols. &nbsp;In a recent Twitter discussion Joe Groff suggested that we don’t need them because we should use an enum when there is a fixed set of conforming types. &nbsp;There are at least two &nbsp;reasons why I still think we *should* add support for closed protocols.</div><div class=""><br class=""></div><div class="">As noted above (and in the previous thread in more detail), if the set of types (cases) isn’t intended to be fixed (i.e. the library may add new types in the future) an enum is likely not a good choice. &nbsp;Using a closed protocol discourages the user from switching and prevents the user from adding conformances that are not desired.</div><div class=""><br class=""></div><div class="">Another use case supported by closed protocols is a design where users are not allowed to conform directly to a protocol, but instead are required to conform to one of several protocols which refine the closed protocol. &nbsp;Enums are not a substitute for this use case. &nbsp;The only option is to resort to documentation and runtime checks.</div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">Proposal:</div><div class=""><br class=""></div><div class="">This proposal introduces the new access modifier `closed` as well as clarifying the meaning of `public` and expanding the use of `open`. &nbsp;This provides consistent capabilities and semantics across enums, classes and protocols.</div><div class=""><br class=""></div><div class="">`open` is the most permissive modifier. &nbsp;The symbol is visible outside the module and both users and future versions of the library are allowed to add new cases, subclasses or conformances. &nbsp;(Note: this proposal does not introduce user-extensible `open` enums, but provides the syntax that would be used if they are added to the language)</div><div class=""><br class=""></div><div class="">`public` makes the symbol visible without allowing the user to add new cases, subclasses or conformances. &nbsp;The library reserves the right to add new cases, subclasses or conformances in a future version.</div><div class=""><br class=""></div><div class="">`closed` is the most restrictive modifier. &nbsp;The symbol is visible publicly with the commitment that future versions of the library are *also* prohibited from adding new cases, subclasses or conformances. &nbsp;Additionally, all cases, subclasses or conformances must be visible outside the module.</div><div class=""><br class=""></div><div class="">Note: the `closed` modifier only applies to *direct* subclasses or conformances. &nbsp;A subclass of a `closed` class need not be `closed`, in fact it may be `open` if the design of the library requires that. &nbsp;A class that conforms to a `closed` protocol also need not be `closed`. &nbsp;It may also be `open`. &nbsp;Finally, a protocol that refines a `closed` protocol need not be `closed`. &nbsp;It may also be `open`.</div><div class=""><br class=""></div><div class="">This proposal is consistent with the principle that libraries should opt-in to all public API contracts without taking a position on what that contract should be. &nbsp;It does this in a way that offers semantically consistent choices for API contract across classes, enums and protocols. &nbsp;The result is that the language allows us to choose the best tool for the job without restricting the designs we might consider because some kinds of types are limited with respect to the `open`, `public` and `closed` semantics a design might require.</div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">Source compatibility:</div><div class=""><br class=""></div><div class="">This proposal affects both public enums and public protocols. &nbsp;The current behavior of enums is equivalent to a `closed` enum under this proposal and the current behavior of protocols is equivalent to an `open` protocol under this proposal. &nbsp;Both changes allow for a simple mechanical migration, but that may not be sufficient given the source compatibility promise made for Swift 4. &nbsp;We may need to identify a multi-release strategy for adopting this proposal.</div><div class=""><br class=""></div><div class="">Brent Royal-Gordon suggested such a strategy in a discussion regarding closed protocols on Twitter:</div><div class=""><br class=""></div><div class="">* In Swift 4: all unannotated public protocols receive a warning, possibly with a fix-it to change the annotation to `open`.</div><div class="">* Also in Swift 4: an annotation is introduced to opt-in to the new `public` behavior. &nbsp;Brent suggested `@closed`, but as this proposal distinguishes `public` and `closed` we would need to identify something else. &nbsp;I will use `@annotation` as a placeholder.</div><div class="">* Also In Swift 4: the `closed` modifier is introduced.</div><div class=""><br class=""></div><div class="">* In Swift 5 the warning becomes a compiler error. &nbsp;`public protocol` is not allowed. &nbsp;Users must use `@annotation public protocol`.</div><div class="">* In Swift 6 `public protocol` is allowed again, now with the new semantics. &nbsp;`@annotation public protocol` is also allowed, now with a warning and a fix-it to remove the warning.</div><div class="">* In Swift 7 `@annotation public protocol` is no longer allowed.</div><div class=""><br class=""></div><div class="">A similar mult-release strategy would work for migrating public enums.</div><div class=""><br class=""></div><div class=""><br class=""></div></body></html>