[swift-evolution] Proposal: Sealed protocols

Kevin Ballard kevin at sb.org
Thu Dec 3 17:10:37 CST 2015

An alternative that would have the same effect is to allow protocols to
require conformance to other protocols with a lesser visibility. Because
no external type could conform to the private/internal protocol
requirement, they can't declare conformance to the public protocol
either. The only way they could conform is if the library adds
conformance to the private protocol to some public class, and then
external code can conform to the public protocol in subclasses, which
seems like a potentially-desirable thing (just yesterday someone asked
on IRC if there was a way to declare a protocol that could only be
conformed-to by UIViewController subclasses).

This would look something like

private protocol Restricted {}

public protocol SealedProto: Restricted {
    // ...

The best part about this proposal is it doesn't require adding any new
syntax or rules to the language, it only requires relaxing the rule that
says a protocol cannot require conformance to another protocol of lesser

-Kevin Ballard

On Thu, Dec 3, 2015, at 02:59 PM, Felix Gabel wrote:
> Hey everyone,
> I hereby propose adding a ’sealed’ modifier for protocols to enable
> library developers to declare protocols that can be only conformed to in
> the scope of the library itself. This is similar to a ’sealed trait’ in
> Scala with the difference that it does not limit the protocol to be only
> applicable in the file it is declared in.
> Example:
> public sealed protocol FooType {
>     // this protocol can be conformed to only in scope of the library
>     itself
>     // it can NOT be conformed to by any object declared outside the
>     library
> }
> What do you think?
> Thank you for your time,
> Felix
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

More information about the swift-evolution mailing list