Can't a public class with generics inherit a public protocol?

I am trying to make UIHostingViewControler inherit from Public Protocol as shown in the code below.
Note that I have added a constraint to Content as a condition for inheritance.

public protocol PublicProtocol {
    func exec()
}

extension UIHostingController: PublicProtocol where Content == MyView {
    func exec() {
        print("exec")
    }
}

When I try to execute this code, the compiler tells me to make the exec method public.
The instructions are as follows.

Method 'exec()' must be declared public because it matches a requirement in public protocol 'PublicProtocol'

However, if you follow the instructions and use public, you will be instructed to use internal.
The instructions are as follows.

Replace 'public' with 'internal'.

I have two questions.

  • Why do I need to use public?
  • Is it my programming error that I get a compile error even though I follow the instructions? Or is this impossible in principle?

Thank you for reading this far.

It looks like MyView is internal. Because UIHostingController is public, its members satisfying public protocol conformances must also be public, but MyView is internal so can’t be exposed publicly.

You’ll have to either make your protocol internal, you MyView public.

I do wonder if conformances on a PublicType<InternalType> should be public though, seems like a bug.

3 Likes