[swift-evolution] Proposal: Sealed protocols

Felix Gabel felix.gabel at me.com
Thu Dec 3 18:24:25 CST 2015


> On Dec 4, 2015, at 12:13 AM, Joe Groff <jgroff at apple.com> wrote:
> 
>> 
>> On Dec 3, 2015, at 2:59 PM, Felix Gabel <felix.gabel at me.com> 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?
> 
> My first reaction is that enums already provide a way to model closed sum types. Enums do have some syntactic disadvantages compared to protocol types, since there's no automatic subtyping with enums like there are with protocols, but it might be worth considering ways we can improve that rather than adding redundant language features.
> 
> -Joe

The problem using an enum in this case is that the overall complexity will increase with every case that is being introduced. Using a sealed protocol a library developer can define a set of conforming types, that is applicable for usage.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151204/30a77aa0/attachment.html>


More information about the swift-evolution mailing list