[swift-evolution] [Proposal] Protected Access Level

Jonathan Hull jhull at gbis.com
Mon May 30 09:22:51 CDT 2016


I kind of want something like protected for protocols, but not necessarily protected as it is implemented in other languages.

I often have a few low-level methods in a protocol which the higher level methods in protocol extensions use to do their work.  Occasionally, I need to guarantee a piece of information in the protocol, but I really don’t want to expose it to everyone.  Right now I do, and then put a warning not to call it directly.

What I would like, is a way to mark a method as needing to be implemented, but allow that implementation to have a lower access level than the protocol itself (e.g. the method is private, but the protocol is public).  I also want extensions to the protocol to be able to see/access those methods.

I don’t know what form this would take. There are lots of options:
- Mark the methods as “hidden” or some other keyword in the protocol
- Treat it like private(set) and allow different access levels to be set when it is accessed from extensions (e.g. a method might be private, but set to internal for extensions… thus you can call the method from within extensions in the module, but not from arbitrary code or extensions outside the module)
- Something I haven’t thought of.

I kind of like the idea of being able to set a specific access level for what extensions see in general.  

Thanks,
Jon


More information about the swift-evolution mailing list