title: Using ForEach(data:,content:) with an @EnvironmentObject in SwiftUI without resorting to trailing closure syntax?
body: <p>I am refactoring my code, and want to use a memberwise initializer with ForEach(data:,content:).</p>
  <p>I have a viewModel with an array of structs that conform to Identifiable:</p>
  <pre><code>class ViewModel: ObservableObject {
      @Published var int = 0
      
      var cellModels: [CellModel]
      
      init() {
          self.cellModels = [CellModel(id: 0, string: &quot;Foo&quot;),
                             CellModel(id: 1, string: &quot;Bar&quot;)]
      }
  }
  
  struct CellModel: Identifiable {
      var id: Int
      var string: String
  }
  </code></pre>
  <p>then I have created a CellView that has a memberwise initialiser from a CellModel and also has an EnvironmentalObject which is an instance of the ViewModel:</p>
  <pre><code>struct CellView: View {
      @EnvironmentObject var environment: ViewModel
      var cellModel: CellModel
      var body: some View {
          Text(cellModel.string)
      }
  }
  </code></pre>
  <p>However when I create the parent view I get the compiler error on the ForEach(data:, content:) line:</p>
  <pre><code>struct DemoView: View {
      @StateObject var viewModel = ViewModel()
      var body: some View {
          VStack {
              ForEach(viewModel.cellModels, content: CellView.init) // Compiler error is here
  // Cannot convert value of type '(EnvironmentObject&lt;ViewModel&gt;, CellModel) -&gt; CellView' to expected argument type '(CellModel) -&gt; CellView'
          }
          .environmentObject(viewModel)
      }
  }
  </code></pre>
  <p>I understand that this is because ForEach(data:,content:) is trying to pass both the EnvironmentObject and the individual model to each CellView, but is there a way to do this <em>without</em> having to use ForEach(data:,content:) with a trailing closure? :</p>
  <pre><code>ForEach(viewModel.cellModels){ cellModel in
                  CellView(cellModel: cellModel)
              }
  </code></pre>