title: Using protocol in SwiftUI for providing "some View" / Generics?
body: <p>I'm trying to get my head something in SwiftUI. I want to build a SwiftUI view and have something you could call a <code>ViewProvider</code> as a <code>@State var</code>. something like this:</p>
  <pre><code>protocol ViewProvider {
      associatedtype ViewOne = View
      associatedtype ViewTwo = View
      
      @ViewBuilder var viewOne: ViewOne { get }
      @ViewBuilder var viewTwo: ViewTwo { get }
  }    
  
  struct ContentView: View {
      @State private var parent: ViewProvider?
      
      var body: some View {
          VStack {
              HStack {
                  Button(action: { parent = Father() }, label: { Text(&quot;Father&quot;) })
                  Button(action: { parent = Mother() }, label: { Text(&quot;Mother&quot;) })
              }
              
              if let p = parent {
                  p.viewOne
                  p.viewTwo
              }
          }
      }
  }
  
  class Father: ViewProvider {
      @ViewBuilder var viewOne: some View {
          Text(&quot;Father One!&quot;)
      }
      
      @ViewBuilder var viewTwo: some View {
          Text(&quot;Father Two!&quot;)
      }
  }
  
  
  class Mother: ViewProvider {
      @ViewBuilder var viewOne: some View {
          Text(&quot;Mother One!&quot;)
      }
      
      @ViewBuilder var viewTwo: some View {
          Text(&quot;Mother Two!&quot;)
      }
  }
  </code></pre>
  <p>This produces 2 different compiler errors.</p>
  <pre><code>@State private var parent: ViewProvider?
  // Protocol 'ViewProvider' can only be used as a generic constraint because it has Self or associated type requirements
  </code></pre>
  <p>and</p>
  <pre><code>p.viewOne
  p.viewTwo
  // 2x Member 'viewOne' cannot be used on value of protocol type 'ViewProvider'; use a generic constraint instead
  </code></pre>
  <p>I have a vague idea of what I'm doing wrong, but no idea on how to solve it :)
  What syntax should I use to get something like this to work?</p>