Swift existential types: how to create generic Views that conform to a protcol ?

Hello,

I am building a SwiftUI macOS app that manipulates a set of views ("playgrounds"). Each playground has some specific info but all share a generic common View. I also want an enum to list all available views in my app.

I struggle with Swift existential types and protocol to model my data structures. In particular, I always end up with the error "Type any View can not conform to protocol View" Despite having read many docs and blog posts on existential types and why a protocol can not conform to itself. I am blocked on this.

Here is my code :

// the protocol all my playgrounds must implement
protocol PlaygroundComponent {
    associatedtype Body: View
    @ViewBuilder var component: Self.Body { get }
}

// a generic type that implements the common part of all the playgrounds + the specific part from subtypes
struct PlaygroundView<Content: PlaygroundComponent>: View {
    let content : Content
    var body: some View {
        Text("reused content")
        content.component
    }
}

// a concrete playground, it defines only the specific part 
struct ListModelsPlayground: PlaygroundComponent {
    var component: some View {
        Text("ListModelsPlayground")
    }
}

// an enum that lists all the implementations I have (only one to keep this example short)
enum Playgrounds  {
    case listModels
    var info: any PlaygroundView<PlaygroundComponent> { ListModelsPlayground() } // ---> compilation error : Type 'any PlaygroundComponent' cannot conform to 'PlaygroundComponent'
}

How can I model a generic view with individual subcomponents to later be part of such s or List( ForEach { } ) SwiftUI views ?

One possible solution to this is to use composition instead of inheritance. The approach is detailed here https://stackoverflow.com/a/77250029/663360 and it works.

However, for my own understanding, I still wonder how an inheritance pattern would work. I understand Swift resolves types at compile time (vs Objective-C, Java, Python, Typescript) and that prevents using existential types. Is there a way to solve my problem through inheritance and protocols ?

Swift existential types: how to create generic Views that conform to a protcol ?
 
 
Q