On Nov 24, 2015, at 06:10 , Roland King <r...@rols.org> wrote:
> 
> How can this be, how can an internal function make a class conform to a 
> public method on a public protocol.

IIRC, until fairly recently (i.e. WWDC 2015 or one of the later Xcode 
releases), it worked as your intuition tells you — it wasn’t allowed.

Thankfully, though, this is now fixed, and even a private class can adopt a 
public protocol, and the adoption is private (meaning that the required methods 
can be private).

I believe you have to consider 3 factors: 

— the access level of the protocol declaration (which is a limit on the 
*visibility* of the protocol to adopters); 
— the access level at which the protocol is *adopted* by a class (which is the 
class access level, constrained to *at most* the protocol visibility access 
level); 
— the access level of the protocol methods implemented in the class, 
constrained to *at least* the adoption access level. 

(I’m not sure I have that exactly right, but it’s something like that.)

So, this works:

> private protocol XP
> {
>       func implementMe ();
> }
> 
> public class YC: XP
> {
>       private func implementMe () {}
> }

and so does this:

> private protocol XP
> {
>       func implementMe ();
> }
> 
> public class YC: XP
> {
>       public func implementMe () {}
> }


but this does not:

> public protocol XP
> {
>       func implementMe ();
> }
> 
> public class YC: XP
> {
>       private func implementMe () {}
> }

Unfortunately, complicated as this is, it’s still not enough. There’s also a 
need (IMO) for a public/internal class to be able to adopt a public/internal 
protocol privately. My example is a class that needs to be public for other 
reasons, and which adopts a delegate protocol that happens to be public, but 
doesn’t want to advertise to its clients that it conforms. (It doesn’t want 
clients maliciously invoking delegate methods. Only the object that it’s a 
delegate for should know) This can’t be done, but it would need something like 
this:

> public protocol XP
> {
>       func implementMe ();
> }
> 
> public class YC: private XP // <<— not currently valid
> {
>       private func implementMe () {}
> }

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to