[swift-evolution] Proposal: Sealed protocols

Douglas Gregor dgregor at apple.com
Thu Dec 3 17:33:05 CST 2015


> On Dec 3, 2015, at 3:13 PM, Mehdi Amini <mehdi.amini at apple.com> wrote:
> 
>> 
>> On Dec 3, 2015, at 3:09 PM, Douglas Gregor <dgregor 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?
>> 
>> 
>> Can you elaborate on why a Swift developer would want to distinguish between “public sealed” and “internal”?
> 
> I can foresee how a whole module optimization would be able to do better devirtualization in this case, knowing the full possible set of type of instance.

Are you perhaps thinking of the different between “public” and “public sealed”? Both “public sealed” and “internal” allow whole module optimization to know the complete set of types that conform to the protocol. In the former case, that’s true because it’s the semantics of “sealed”; in the latter case, that’s true because the protocol isn’t visible outside of the module.

	- Doug

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151203/9a985d81/attachment.html>


More information about the swift-evolution mailing list