title: Return a view from a closure in SwiftUI
body: <p>I have a generic section view component, that should be initialised to return some view for edit of the section and another one for adding elements to the section. </p>
  
  <pre class="lang-swift prettyprint-override"><code>import SwiftUI
  
  enum ProfileSectionType {
      case editable
      case addable
  }
  
  struct ProfileSection&lt;Content&gt; : View where Content : View {
  
      var model:ProfileSectionModel? = nil
      var sectionType:ProfileSectionType = .editable
      var onClick:(() -&gt; View)? = nil
      var content: Content
  
      @inlinable public init(_ model:ProfileSectionModel? = nil, sectionType:ProfileSectionType = .editable, @ViewBuilder content: () -&gt; Content) {
          self.model = model
          self.content = content()
          self.sectionType = sectionType
      }
  
      var body : some View {
          switch sectionType {
          case .editable:
              return AnyView(editable())
          case .addable:
              return AnyView(addable())
          }
      }
  }
  </code></pre>
  
  <p>But it is not possible to use a closure like <code>(() -&gt; View)</code>. I could have passed it as an parameter to the init, but then I would not get lazy loading of the view. </p>
  
  <p>I have also tried to use generic in the enum. My first attempt were something like: </p>
  
  <pre><code>enum ProfileSectionType&lt;T : View&gt; {
      case editable
      case addable(viewForAdding:T)
  }
  </code></pre>
  
  <p>But that seemed to make it more complex. How can get this section to create two different views for edit and add? </p>
  
  <p>I want to use it like this: </p>
  
  <pre><code>ProfileSection(m.identifications(), sectionType: .addable, onClick: ^{
      AddIdentificationView()
  }){
      ForEach( 0 ..&lt; m.identifiers().count ) {
          ProfileEditableItem(key: m.identifiers()[$0].title, value: m.identifiers()[$0].value)
      }
  }
  </code></pre>